Approach
Use a two-pass frequency counting strategy:
- Count each character occurrence in
s. - Scan
sfrom left to right. - Return the first index where the character frequency is
1. - If no such character is found, return
-1.
Why this works:
- A character is unique exactly when its total count is
1. - The second pass preserves left-to-right order, so the first match is the required answer.
Implementation
export function firstUniqChar(s: string): number {
const frequency = new Map<string, number>();
for (const char of s) {
frequency.set(char, (frequency.get(char) ?? 0) + 1);
}
for (let i = 0; i < s.length; i++) {
const char = s[i];
if (frequency.get(char) === 1) return i;
}
return -1;
}Complexity
- Time O(n): one pass to count and one pass to find the first unique index.
- Space O(1): bounded alphabet (
'a'to'z') means constant extra storage.
Test Coverage
The test suite validates:
- LeetCode baseline examples
- Empty and single-character inputs
- Unique character at start, middle, and end
- All-duplicated strings returning
-1 - Large deterministic lowercase inputs