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/