easyHash maps and sets target 15 min
First unique character
In a string s, call a character unique if it appears exactly once in the whole string. Return the index (the position, counting from 0) of the leftmost unique character. If no character is unique, return -1.
Example 1
Input
s = "swiss"Output1s appears three times. w, at index 1, appears once. i at index 2 is unique too, but w comes first.
Example 2
Input
s = "level"Output2l and e each appear twice. v, at index 2, appears once.
Example 3
Input
s = "noon"Output-1n and o each appear twice, so no character is unique.
Constraints
0 ≤ len(s) ≤ 2 · 105
sholds only the lowercase letters a to z.
Plan it first
Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.