Python solution


  • 0
    L
    class Solution(object):
        def firstUniqChar(self, s):
            """
            :type s: str
            :rtype: int
            """
            
            if len(s) == 0:
                return -1
            
            if len(s) == 1:
                return 0
                
            char_count = [0] * 26
            
            # Scan through to populate character counts
            for i in range(len(s)):
                char_count[ord(s[i]) - 97] += 1
                
            # Scan again looking for first character with character count of 1
            for i in range(len(s)):
                if char_count[ord(s[i]) - 97] == 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.