Solution 1
class Solution:
def firstUniqChar(self, s: str) -> int:
# build hash map : character and how often it appears
count = collections.Counter(s)
class Solution:
"""
@param s: a string
@return: it's index
"""
def firstUniqChar(self, s):
# write your code here
count = collections.Counter(s)
#alp = {}
# for c in s:
# if c not in alp:
# alp[c] = 1
# else:
# alp[c] += 1
# find the index
for i in range(len(s)):
if count[s[i]] == 1:
return i
return -1
Time complexity is O(n). Space complexity is O(n).