Introduction
LeetCode 387 – First Unique Character in a String is one of the most common beginner-friendly string and hashing interview problems.
This problem helps developers understand:
- Frequency counting
- HashMap usage
- String traversal
- Character manipulation
- Efficient lookup techniques
It is frequently asked in coding interviews because it tests both:
- Logical thinking
- Time complexity optimization
Problem Link
🔗 https://leetcode.com/problems/first-unique-character-in-a-string/
Problem Statement
Given a string:
Return:
If no unique character exists:
Example 1
Input
Output
Explanation:
appears only once and is the first unique character.
Example 2
Input
Output
Explanation:
is the first character that appears only once.
Example 3
Input
Output
All characters repeat.
Intuition
To identify the first unique character:
We first need to know:
A HashMap is perfect for frequency counting.
After counting:
We traverse the string again and return the first character whose frequency is:
Brute Force Approach
Idea
For every character:
- Traverse the entire string
- Count occurrences
- Return the first character with count = 1
Brute Force Complexity
Time Complexity
because nested traversal is required.
Space Complexity
Optimized HashMap Approach
Idea
Use a HashMap to store:
Then scan the string again.
The first character with frequency:
is the answer.
Java Solution
Cleaner Optimized Version
Why This Works
The algorithm works in two passes.
First Pass
Store frequency of every character.
Example:
Frequency map:
Second Pass
Traverse string from left to right.
The first character whose frequency equals:
is returned.
Dry Run
Input
Step 1: Frequency Counting
Step 2: Traverse Again
Index 0:
Index 1:
Index 2:
Return:
Time Complexity Analysis
Optimized HashMap Solution
Time Complexity
because string traversal happens twice.
Space Complexity
Only lowercase English letters exist.
Maximum storage remains fixed.
Brute Force vs Optimized
| Approach | Time Complexity | Space Complexity |
| Brute Force | O(N²) | O(1) |
| HashMap / Frequency Array | O(N) | O(1) |
Interview Explanation
In interviews, explain:
We use a frequency map to count occurrences of every character. Then we scan the string again to locate the first character whose frequency is exactly one.
This demonstrates:
- Hashing knowledge
- Frequency counting
- Efficient lookup usage
- Optimization skills
Common Mistakes
1. Returning Character Instead of Index
The problem asks for:
not the character itself.
2. Using Nested Loops
Nested traversal increases complexity unnecessarily.
3. Forgetting Second Traversal
HashMap only stores frequencies.
You still need:
to find the first unique character.
FAQs
Q1. Why use HashMap?
Because frequency lookup becomes very efficient.
Q2. Can we use arrays instead?
Yes.
Since only lowercase letters exist:
works perfectly.
Q3. Why traverse the string twice?
First pass counts frequency.
Second pass preserves original order.
Q4. Is this problem important for interviews?
Yes.
It is one of the most common hashing interview questions.
Related Problems
Practice these next:
Conclusion
LeetCode 387 is an excellent beginner-level problem for learning:
- Frequency counting
- HashMap usage
- String traversal
- Efficient lookup techniques
The key insight is:
Count character frequencies first, then find the first character with frequency equal to one.
This pattern is widely used in many real interview problems involving strings and hashing.





