You are given a single line containing a string s made of lowercase English letters. A character is unique if it appears exactly once in s.
Find the leftmost unique character and print its 0-based index. If no character is unique (or the string is empty), print -1.
A single line containing the string s. The line may be empty, in which case s is the empty string.
A single integer: the 0-based index of the first unique character, or -1 if there is no unique character.
s ≤ 100000s consists only of lowercase English letters (a–z).Example 1
Input
stress
Expected
1
Explanation
Frequencies: s=3, t=1, r=1, e=1. Scanning left to right, index 0 is 's' (repeats), index 1 is 't' which occurs exactly once, so the answer is 1.
Example 2
Input
abba
Expected
-1
Explanation
Every character ('a' twice, 'b' twice) repeats, so there is no unique character and the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →