관리 메뉴

솜씨좋은장씨

[leetCode] 387. First Unique Character in a String (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 387. First Unique Character in a String (Python)

솜씨좋은장씨 2020. 6. 15. 18:48
728x90
반응형

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

 

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

Solution

from collections import Counter

class Solution:
    def firstUniqChar(self, s: str) -> int:
        answer = -1
        s = list(s)
        cnt_dict = Counter(s)
        i = 0
        
        for word in s:
            if cnt_dict[word] == 1:
                answer = i
                break
            i = i + 1
                
        return answer

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments