Introduction
The First Unique Character in a String problem involves finding the first non-repeating character in a string.
Given a string s, the task is to:
- find the first character that appears only once
- return its index
If no unique character exists:
- return -1
This problem helps in understanding:
- hashing
- frequency counting
- string traversal
- efficient searching
Example
Input:s = "leetcode"
Output:
0
Explanation:
'l' appears only once
and is the first unique character.
Input:
s = "aabb"
Output:
-1
Explanation:
All characters repeat.
No unique character exists.
Constraints
1 <= s.length <= 10^5s contains lowercase English lettersApproach 1 : Brute Force
Explanation
The simplest way to solve this problem is:
- Traverse every character
- Count its frequency
- Return index if frequency becomes 1
This approach works but repeated counting increases time complexity.
Steps
- Traverse string characters.
- Count frequency of current character.
- If frequency becomes 1:
- return index
- Continue traversal otherwise.
- Return -1 if no unique character exists.
Dry Run
Input:s = "leetcode"
Check l: Frequency = 1
Unique character found
Index = 0
Final Result:
0
Brute Force Code
Complexity Analysis
Time Complexity: O(n²)Explanation:
Nested traversal is used for frequency counting.
Space Complexity: O(1)
Explanation:
No extra data structures are used.
Approach 2 : Optimized Solution (Hashing)
Explanation
The optimized solution uses frequency counting with hashing.
The idea is:
- Count frequency of every character
- Traverse string again
- Return first character having frequency 1
This avoids repeated counting.
Steps
- Create frequency hashmap.
- Count character frequencies.
- Traverse string again.
- Return first index having frequency 1.
- Return -1 otherwise.
Dry Run
Input:s = "leetcode"
Store frequencies:
l → 1
e → 3
t → 1
c → 1
o → 1
d → 1
Traverse string:
l has frequency 1
Return index 0
Final Result:
0
Optimized Code
Complexity Analysis
Time Complexity: O(n)Explanation: String is traversed twice. Space Complexity: O(n)
Explanation: Extra hashmap is used for frequencies.
Edge Cases
- All characters repeat
- First character unique
- Last character unique
- String contains one character
- Large input size
Why This Problem is Important
This problem helps in understanding:
- Hashing
- Frequency counting
- Efficient searching
- String traversal
- Duplicate handling
It is one of the most common hashing interview problems.
Real-World Applications
Unique character detection is used in:
- Data validation
- Compiler design
- Search engines
- Pattern matching
- Text analytics systems
Common Mistakes
- Incorrect frequency counting
- Returning character instead of index
- Forgetting second traversal
- Missing no-answer case
Interview Tips
Interviewers often expect:
- Hashing optimization
- Proper frequency logic
- O(n) solution explanation
Always explain why second traversal preserves original order.
Related Questions
- Valid Anagram
- Frequency Count
- Ransom Note
- Character Frequency
- Single Number
Final Takeaway
The First Unique Character in a String problem is a fundamental hashing problem that teaches frequency counting and efficient lookup techniques. Understanding this problem builds a strong foundation for advanced hashing and string processing interview problems.