First Character That Appears Once
EasyStringsHash map223 tests
You get a string s of lowercase English letters.
Find the first character (from the left) that appears exactly once in the whole string. Return its index. Indexes start at 0.
If no character appears exactly once, return -1.
Hint
First count how many times each letter appears. Then walk the string again from the left.
Examples
Example 1
- Input:
- "racecar"
- Output:
- 3
r, a and c each appear twice. e appears once, at index 3.
Example 2
- Input:
- "swiss"
- Output:
- 1
s appears three times. w appears once, at index 1. i also appears once, but it comes later.
Example 3
- Input:
- "aabbcc"
- Output:
- -1
Every letter appears twice, so no letter appears exactly once.
Constraints
1 <= s.length <= 30scontains only lowercase English letters.
Run checks your code against the 3 examples. Submit checks it against all 223 tests and needs a free account. You can also try ideas in the free JavaScript or Python compiler.