Python 6-lines O(n) solution using Counter

  • 0
    class Solution(object):
        def firstUniqChar(self, s):
            from collections import Counter
            char_counts = Counter(s)
            for i, c in enumerate(s):
                if char_counts[c] == 1:
                    return i
            return -1

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.