Back to Problem
Official
1564 views
Editorial: First Unique Character
by admin ·
22
1 Intuition
Count first, then find.
2 Approach
One pass to count frequencies, a second pass to return the first index whose character has count 1.
3 Complexity Analysis
Time O(n), Space O(1) — at most 26 keys.
4 Solution Code
class Solution:
def firstUniqChar(self, s: str) -> int:
count = {}
for c in s:
count[c] = count.get(c, 0) + 1
for i, c in enumerate(s):
if count[c] == 1:
return i
return -1
5 Code Walkthrough
The reference solution below implements the approach step by step.