Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.
Note: You may assume the string contain only lowercase letters.

My code:

class Solution(object):
    def firstUniqChar(self, s):
        """
        :type s: str
        :rtype: int
        """
        tmp_dict={}
        for item in s:
            if item not in tmp_dict:
                tmp_dict[item]=1
            else:
                tmp_dict[item]+=1
        for i in range(len(s)):
            if tmp_dict[s[i]]==1:
                return i
        return -1
        

Solution: https://leetcode.com/problems/first-unique-character-in-a-string/