[알고리즘/leetcode] First Unique Character in a String(python)

유현민·2022년 8월 27일
0

알고리즘

목록 보기
240/253

counter를 이용하여 각 알파벳 빈도수를 계산.
카운터 순으로 정렬이 되지만 같은 빈도수를 가진 것들은 먼저 계산된 것부터 순서가 정렬된다.
for 문으로 1인 것 검색.

from collections import Counter
class Solution:
    def firstUniqChar(self, s: str) -> int:
        c = Counter(s)
        for i in range(len(s)):
            if c[s[i]] == 1:
                return i
        return -1
profile
smilegate megaport infra

0개의 댓글