387. First Unique Character in a String
Asked at Goldman Sachs
Difficulty
Easy
Time
O(n)
Space
O(1)
Pattern
Hash Table
Companies
1
Problem
You are given a string s. Find the first character that appears exactly once in the whole string, and return its index. If no character is unique, return -1.
"First" means the smallest index. So even if several characters appear only once, you must return the position of the leftmost one. A character is unique only if its total count in the string is exactly 1.
A simple way to solve it: count how many times each letter occurs, then walk the string from left to right and stop at the first letter whose count is 1.
Find the leftmost letter that appears once
Constraints
- ●
1 <= s.length <= 10^5 - ●
s consists of only lowercase English letters
Examples
s = "leetcode"0s = "loveleetcode"2s = "aabb"-1Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Goldman Sachs | Easy | View all Goldman Sachs questions → |
How to Think About It
Brute force: for each index, scan the whole string to see if that character appears elsewhere. That is O(n²).
Key insight: uniqueness depends on the whole string, but "first" depends on order. So do two passes — one to count, one to find the first character whose count is 1.
Use a 26-length array for lowercase letters. The second pass walks the string (not the alphabet), which preserves the original order.
Walkthrough for "loveleetcode": counts l=2, o=2, v=1, e=4, .... Scanning: l (2), o (2), v (1) -> answer index 2.
Streaming follow-up: if characters arrive one by one, keep counts plus a queue of candidates and pop from the front while the front is no longer unique.
Optimal Approach
Step 1: count = [0] * 26; count every character.
Step 2: For i in 0..n-1: if count[s[i]] == 1, return i.
Step 3: Return -1.
Time: O(n). Space: O(1) (26 counters).
Count first, then scan in order
What Trips People Up in Real Interviews
Scanning the alphabet in the second pass instead of the string. That finds the alphabetically first unique letter, not the first by position.
Returning the character instead of its index.
Using s.count(ch) inside a loop — that hides an O(n²) algorithm behind a one-liner.
Forgetting to return -1 when every character repeats.
Solution Code
O(n) timedef firstUniqChar(s):
count = [0] * 26
for ch in s:
count[ord(ch) - ord('a')] += 1
for i, ch in enumerate(s):
if count[ord(ch) - ord('a')] == 1:
return i
return -1Frequently Asked Questions
What is the First Unique Character in a String problem?
First Unique Character in a String asks for the index of the first character that appears exactly once, or `-1` if every character repeats. It is a two-pass counting problem — the classic "count first, then scan in order" pattern.
How do you solve First Unique Character in a String?
The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask First Unique Character in a String?
First Unique Character in a String is asked at Goldman Sachs. It is a easy difficulty problem.
What are common mistakes on First Unique Character in a String?
- Scanning the alphabet in the second pass instead of the string. That finds the alphabetically first unique letter, not the first by position.
- Returning the character instead of its index.
- Using `s.count(ch)` inside a loop — that hides an `O(n²)` algorithm behind a one-liner.
- Forgetting to return `-1` when every character repeats.