Find the First Non-Repeating Character
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
[ "leetcode" ]
Explanation. The character 'l' is the first non-repeating character, and it is at index 0.
[ "loveleetcode" ]
Explanation. The characters 'l' and 'o' repeat. The character 'v' is the first non-repeating character, and it is at index 2.
[ "aabb" ]
Explanation. All characters ('a' and 'b') repeat, so there is no non-repeating character.
[ "z" ]
Explanation. The character 'z' is the only character and it does not repeat, so it's at index 0.
[ "" ]
Explanation. An empty string contains no characters, thus no non-repeating character.
[ "ccbbaaffggd" ]
Explanation. All characters except 'd' repeat. 'd' is at index 10 and is the first non-repeating character.
Follow-up: Can you solve this problem in a single pass? What if the string can contain uppercase letters, numbers, or special characters? How would your approach adapt?
The input string `s` consists only of lowercase English letters. The length of `s` is between 0 and 10^5.
- Views
- 4