Python solution O(n)


  • 0
    P
    class Solution(object):
        def firstUniqChar(self, s):
            """
            :type s: str
            :rtype: int
            """
            map = {}
            for i in range(len(s)):
                if s[i] in map:
                    map[s[i]].append(i)
                else:
                    map[s[i]] = [i]
            
            first = float('inf')
            for m in map:
                if len(map.get(m)) == 1:
                    first = min(first, map.get(m)[0])
            
            return first if first != float('inf') else -1
    

Log in to reply
 

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