First Unique Character
Return the index of the first lowercase letter appearing exactly once, or -1.
EXECUTION LAB
Return the index of the first lowercase letter appearing exactly once, or -1.
Input and constraints
- Use lowercase letters encoded as 0 (a) through 25 (z).
- Use at most 32 integers. Each numeric input is between -1000 and 1000.
Code inspector
def solve(values, target): counts = [0] * (26) i = 0 while (i < len(values)): letter = values[i] counts[letter] = (counts[letter] + 1) i = (i + 1) i = 0 while (i < len(values)): if (counts[values[i]] == 1): return i i = (i + 1) return -1Visualizer
Step 1 of 703i
11
23
32
Increase this letter's frequency
Increase this letter's frequency.
Variable watch
i 0letter 3
Step explanation
Increase this letter's frequency.
comparisons 1reads 1writes 0swaps 0recursiveCalls 0
Educational simulation · O(n + 26) time · O(26) space
Worked explanation
After tallying votes, revisit the original queue to find the earliest solo vote.
Guided solution
Count every character before deciding which occurrence is unique.
O(n + 26) time · O(26) auxiliary space
Examples
Worked example · {"values":[3,1,3,2],"target":0} ⇒ 1
Edge case 1 · {"values":[0,0],"target":0} ⇒ -1
Edge case 2 · {"values":[],"target":0} ⇒ -1
Progressive hints
Hint 1
After tallying votes, revisit the original queue to find the earliest solo vote.Hint 2
Count every character before deciding which occurrence is unique.Hint 3
Step through the worked example and inspect the highlighted variables before each update.Common mistake
Iterating the alphabet returns the smallest unique character, which can occur later.
Interview follow-up
What information must a streaming solution retain?