First Unique Character
easyStrings II+10 XPTime limit 2s
A shop logs each customer's favourite item as one letter. Find the first letter that appears exactly once in the log and print its index (counting from 0). If every letter repeats, print -1.
For swiss the answer is 1 (w): s repeats, but w appears only once.
Input: one line with a string of lowercase letters.
Output: the 0-based index of the first non-repeating character, or -1.
Constraints: 1 ≤ length ≤ 10⁵.
Think about it: two passes. First count how many times each letter occurs (an array of 26 counters). Then walk the string again and return the first index whose letter has count 1.
Sample input 1
swiss
Sample output 1
1
Sample input 2
aabb
Sample output 2
-1
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).