First unique character

Find the first character in a string that appears exactly once.

Input

One line: a string of 1 to 3 × 10⁵ lower-case letters. The slow way (counting a character by scanning the whole string again) is too slow at this size: count every character once instead.

Output

The position (from 0) of the first character that appears exactly once, or -1.

Examples

Example 1 · Input
leetcode
Expected output
0
Example 2 · Input
aabb
Expected output
-1

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Count every character first: a dictionary or collections.Counter.

Hint 2

Then walk the string again and stop at the first character with count 1.

Hint 3

Print -1 if there is none.

Solution

Show one correct solution (try it yourself first)
from collections import Counter

s = input()
counts = Counter(s)
print(next((i for i, ch in enumerate(s) if counts[ch] == 1), -1))

Lesson: Arrays and hashing →