Python solution using subclass of Counter and OrderedDict


  • 0
    M

    Slow but maybe interesting.

    class Solution(object):
        def firstUniqChar(self, s):
            """
            :type s: str
            :rtype: int
            """
            from collections import Counter, OrderedDict
    
            class OrderedCounter(Counter, OrderedDict):
                pass
    
            c = OrderedCounter(s)
            for i, j in c.items():
                if j == 1:
                    letter = i
                    break
            else:
                return -1
            return s.index(letter)

Log in to reply
 

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