Easy to understand Python solution


  • 0
    P
    from collections import Counter
    
    def firstUniqChar(self, s):
        char_dict = Counter(list(s))
            
        for i, ch in enumerate(s):
            char_dict[ch] -= 1
            if not char_dict[ch]:
                return i
            char_dict[ch] += 1
        
        return -1
    

  • 0

    @pankesh said in Easy to understand Python solution:

    you do not need to do that minus/plus one calculation at all

    def firstUniqChar(self, s):
        char_dict = collections.Counter(list(s))
        for i, ch in enumerate(s):
            if char_dict[ch] == 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.