First Non-Repeating Character
Problem Description
Given a string s, find the first non-repeating character and return its index. If none exists, return -1.
Example 1:
Input: s = "leetcode"
Output: 0 ('l' at index 0)
Example 2:
Input: s = "loveleetcode"
Output: 2 ('v' at index 2)
Example 3:
Input: s = "aabb"
Output: -1
DE Shaw Tier 2 — LC 387.
Optimal O(n): frequency map pass 1, then linear scan for first char with count 1.
Streaming variant: use a queue of (char, index) pairs — pop front when its count > 1. Top of queue is always the current first unique.
Example Test Cases
Example 1
Input: ["leetcode"]
Expected: 0
Example 2
Input: ["loveleetcode"]
Expected: 2
Example 3
Input: ["aabb"]
Expected: -1
Run your code to see the results