easyArrays & HashingPure DSA~12 min
First Unique Char In Log
A log-compaction routine wants the first character in a log line that appears exactly once, to use as a quick fingerprint. Return its index, or -1 if every character repeats.
Problem
Given a string s, return the index of the first character that appears exactly once. If no such character exists, return -1.
Input
A string s of length n (1 ≤ n ≤ 10^5), lowercase ASCII letters.
Output
A single integer — the index of the first unique character, or -1.
Constraints
- 1 ≤ n ≤ 100,000
- Only lowercase English letters
- Index is 0-based
Examples
Example 1
Input
s = "leetcode"
Output
0
'l' appears once and is the earliest such character.
Example 2
Input
s = "loveleetcode"
Output
2
'l', 'o', 'e' repeat; 'v' at index 2 is the first unique character.