First Unique Character in a String - Problem

You are given a string s consisting of lowercase English letters. Your task is to find the first character that appears exactly once in the string and return its index position.

If no such unique character exists, return -1.

Example: In the string "leetcode", the character 'l' appears once at index 0, but 'e' appears multiple times. The first unique character is 't' at index 2.

This is a classic string processing problem that tests your ability to efficiently track character frequencies and find the optimal solution using hash tables.

Input & Output

example_1.py โ€” Basic case with unique character
$ Input: s = "leetcode"
โ€บ Output: 0
๐Ÿ’ก Note: The character 'l' appears only once in the string at index 0, making it the first unique character.
example_2.py โ€” First unique not at beginning
$ Input: s = "loveleetcode"
โ€บ Output: 2
๐Ÿ’ก Note: The characters 'l', 'o', and 'e' appear multiple times. The first character that appears exactly once is 'v' at index 2.
example_3.py โ€” No unique characters
$ Input: s = "aabb"
โ€บ Output: -1
๐Ÿ’ก Note: All characters appear more than once, so there is no unique character. Return -1.

Visualization

Tap to expand
Detective's Badge InvestigationThe Queue InvestigationLEETCODE01234567Detective's Notebook:L: 1 โœ“E: 3 โœ—T: 1 โœ“C: 1 โœ“O: 1 โœ“D: 1 โœ“Investigation Steps:1. First Pass: Count each badge number (survey the entire queue)2. Second Pass: Check from front, find first person with unique badge3. Result: Person at position 0 has badge 'L' (appears only once)๐ŸŽฏ Investigation Complete!The first person with a unique badge is at position 0 (badge 'L')Time Complexity: O(n) - Two passes through the queueGreen circles = Unique badges, Red circles = Duplicate badges
Understanding the Visualization
1
Survey the Queue
Walk through once and record how many times each badge number appears
2
Find the First Unique
Walk through again from the front, checking your records to find the first person with a unique badge
3
Report Position
Return the position of that person, or -1 if everyone has duplicate badges
Key Takeaway
๐ŸŽฏ Key Insight: By building a frequency map first, we can efficiently identify unique characters without redundant counting, making this a classic two-pass optimization pattern.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Two passes through the string, O(n) + O(n) = O(n)

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Hash table stores at most k unique characters (26 for lowercase letters)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists of only lowercase English letters
  • The string is guaranteed to be non-empty
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
68.4K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen