15Find the first non-repeating character
easyAbout 15 minutesCodinghash-mapstrings
Problem
Implement firstUniqChar(s). Return the index of the first character that appears exactly once in s. If there is none, return -1.
firstUniqChar("leetcode"); // 0 ("l")
firstUniqChar("loveleetcode"); // 2 ("v")
firstUniqChar("aabb"); // -1Requirements
- Return an index into the string, not the character.
- Solve it in O(n) time.
Try it yourself
Write your solution, run it, then submit to check it against the tests.
solution.jsJavaScript
Press Run or ⌘ Enter to execute your code.
Step-by-step
First pass: build a Map from character to the number of times it appears.
Unlock the full solution
What's inside
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Tests
Includes 2 code listings, 4 edge cases, 3 follow-up questions.
One-time payment by UPI, card or net banking. Launch price.