Leetcode Question
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
Solution (Python)
class Solution:
def firstUniqChar(self, s: str) -> int:
chardict = {}
for i in range(0,len(s)):
#print(s[i])
if s[i] in chardict:
value = chardict[s[i]][0]
index = chardict[s[i]][1]
chardict.update({s[i]:[value+1,index]})
else:
chardict.update({s[i]:[1,i]})
#print(chardict)
ans = len(s)
for key,value in chardict.items():
if value[0]==1 and value[1]<ans:
ans = value[1]
if ans!=len(s):
return ans
return -1
Cracking the Coding Interview: 189 Programming Questions and Solutions