hashing · Problem 3 of 5
First Unique Character
medium
Amazon
Bloomberg
Return the index of the first character in s that appears exactly once, or -1 if there is none.
firstUniqChar("leetcode") -> 0
firstUniqChar("aabb") -> -1Your solution
Runs your code and animates it without grading anything. Change the input to see what it does on a case the tests do not cover.
Running is free — Submit is what records it. Or press ⌘↩
Tests
4 cases, 1 hidden| call | type | expected | result |
|---|---|---|---|
| firstUniqChar("leetcode") | first is unique | 0 | — |
| firstUniqChar("loveleetcode") | later unique | 2 | — |
| firstUniqChar("aabb") | none unique | -1 | — |
| withheld | hidden | withheld | — |
Hidden cases run too — their inputs aren't listed here, so aim for a general solution rather than one fitted to the cases above.
Complexity
- target time
- O(n)
- target space
- O(k) for k distinct characters
Two passes are still linear. One pass cannot work: uniqueness is only knowable once the whole string has been seen.
Hints
Stuck? Hints open one at a time, each giving a little more away.
2 hints left