First Unique Character in a String
franklinqin0 Hash TableStringQueue
Solution
HashMap
class Solution(object):
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
from collections import defaultdict
cnt = defaultdict(int)
for c in s:
cnt[c] += 1
for i in range(len(s)):
if cnt[s[i]] == 1:
return i
return -1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17