Algobase
Problems
Get Premium
Pricing
Problems
/
256. First Unique Char in String
Prev
Next
Visualizer
Problem
Solution
Code
leetcode (first at 0)
loveleetcode (first at 2)
aabbcc (none)
⌥
Visualizer will appear here
Problem
Solution
Code
1
def first_uniq_char(s):
2
counts = {}; q = []
3
for i, c in enumerate(s):
4
counts[c] = counts.get(c, 0) + 1
5
q.append((c, i))
6
while q and counts[q[0][0]] > 1:
7
q.pop(0)
8
return q[0][1] if q else -1
Visualizer
leetcode (first at 0)
loveleetcode (first at 2)
aabbcc (none)
⌥
Visualizer will appear here