322. First Unique Character in a String
Given a string s of lowercase English letters, return the 0-based index of the first character that appears exactly once in the entire string. If every character repeats somewhere, return -1.
"First" means leftmost by position in s — not alphabetically first. A character with a duplicate anywhere in the string (before or after it) does not count.
Example 1:
Input: s = "swiss"
Output: 1
Explanation: s appears three times and is out. w at index 1 appears exactly once, and nothing unique sits to its left, so the answer is 1 (i, also unique, comes later).
Example 2:
Input: s = "aabb"
Output: -1
Explanation: Both letters occur twice, so no character qualifies.
Constraints:
- 1 ≤ s.length ≤ 10⁵
- s consists of lowercase English letters only.
Hints:
A character qualifies when its total count in the whole string is exactly 1. Can you know every character's count before deciding anything?
Two passes: first tally all 26 counts, then rescan s from the left and return the first index whose letter has count 1. The second pass over s — not over the alphabet — is what makes "first" mean leftmost.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: s = "swiss"
Expected output: 1