The first character in the string that appears once is our answer. So, we use two passes: (1) Create the map {char -> count} (2) Find first character having count = 1.
Time: , space:
.
class Solution:
def firstUniqChar(self, s: str) -> int:
s_map = {}
for c in s:
s_map[c] = s_map.get(c, 0) + 1
for i, c in enumerate(s):
if s_map[c] == 1:
return i
return -1
Leave a comment