First Non-Repeating Character Index
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
Example 1:
Input: s = "leetcode"
Output: 0
Explanation: 'l' is the first character that appears only once, and its index is 0.
Example 2:
Input: s = "loveleetcode"
Output: 2
Explanation: 'l' appears twice, 'o' appears twice. 'v' is the first character that appears only once, and its index is 2.
Example 3:
Input: s = "aabb"
Output: -1
Explanation: All characters appear multiple times.
[ "leetcode" ]
Explanation. The character 'l' appears only once at index 0. 'e' appears multiple times.
[ "loveleetcode" ]
Explanation. The characters 'l' and 'o' repeat. The character 'v' appears only once at index 2.
[ "aabb" ]
Explanation. All characters ('a' and 'b') repeat, so there is no non-repeating character.
[ "abcabcde" ]
Explanation. Characters 'a', 'b', 'c' all repeat. The character 'd' appears only once at index 6.
[ "z" ]
Explanation. The character 'z' appears only once at index 0.
[ "" ]
Explanation. An empty string contains no characters, thus no non-repeating character.
Follow-up: Can you solve this problem using only one pass through the string?
The string `s` will consist only of lowercase English letters. The length of `s` will be between `0` and `10^5`.
- Views
- 4