leetcode-easy-string-387 First Unique Character in a String

mycode  24.42%

class Solution:
    def firstUniqChar(self, s: str) -> int:
        dic = {}
        for i in range(len(s)):
            dic[s[i]] = dic.get(s[i],0) + 1 
        for i,val in dic.items():
            if val == 1:
                return s.index(i)
        return -1

参考

class Solution:
    def firstUniqChar(self, s: str) -> int:
        # # two-pass
        # ctr = collections.Counter(s)
        # for i,v in enumerate(s):
        #     if ctr[v] == 1:
        #         return i
        # return -1
        
        letters='abcdefghijklmnopqrstuvwxyz'
        index=[s.index(l) for l in letters if s.count(l) == 1]
        return min(index) if len(index) > 0 else -1
原文地址:https://www.cnblogs.com/rosyYY/p/10996222.html