[Easy] 387. First Unique Character in a String
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
Examples
Example 1:
Input: s = "leetcode"
Output: 0
Explanation: The character 'l' at index 0 is the first character that does not repeat.
Example 2:
Input: s = "loveleetcode"
Output: 2
Explanation: The character 'v' at index 2 is the first character that does not repeat.
Example 3:
Input: s = "aabb"
Output: -1
Explanation: All characters repeat, so return -1.
Constraints
1 <= s.length <= 10^5sconsists of only lowercase English letters.
Thinking Process
- Bit Manipulation: Efficient way to track seen-once vs seen-multiple using XOR and AND operations
- Strings often need frequency maps or two-pointer scans.
- Watch index bounds and empty-string edge cases.
- Stack helps with nested or repeated patterns.
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| Two pointers on string (this problem) | O(n) | O(1) | Palindrome, parsing |
| Hash map / frequency | O(n) | O(k) | Anagram, character counts |
| KMP / rolling hash | O(n) | O(n) | Pattern matching |
| Stack parsing | O(n) | O(n) | Decode string, parentheses |
Solution
class Solution {
public:
int firstUniqChar(string s) {
int once = 0, multi = 0;
int idx = -1;
for(char ch : s) {
int bit = 1 << (ch - 'a');
multi |= once & bit;
once ^= bit;
once &= ~multi;
}
for(int i = 0; i < (int)s.length(); i++) {
int bit = 1 << (s[i] - 'a');
if(once & bit) {
return i;
}
}
return -1;
}
};
Solution Explanation
Approach: Two pointers on string (this problem)
Key idea: 1. Bit Manipulation: Efficient way to track seen-once vs seen-multiple using XOR and AND operations
How the code works:
- Bit Manipulation: Efficient way to track seen-once vs seen-multiple using XOR and AND operations
- Strings often need frequency maps or two-pointer scans.
- Watch index bounds and empty-string edge cases.
- Stack helps with nested or repeated patterns.
Walkthrough — input s = "leetcode", expected output 0:
The character ‘l’ at index 0 is the first character that does not repeat.
Common Mistakes
- All unique:
s = "abc"→ return0 - All duplicate:
s = "aabb"→ return-1 - Single character:
s = "a"→ return0 - Unique at end:
s = "aabbc"→ return4 - Unique in middle:
s = "aabcc"→ return2(‘b’) -
Long string: All characters unique, return
0 - Wrong bit operations: Incorrect XOR/AND logic in bit manipulation
- Index confusion: Not tracking first occurrence correctly
- Off-by-one: Incorrect character to index conversion
- Not handling all duplicates: Forgetting to return
-1 - Wrong order: Returning last unique instead of first
Related Problems
- LC 383: Ransom Note - Character frequency counting
- LC 389: Find the Difference - Find extra character
- LC 451: Sort Characters By Frequency - Sort by frequency
- LC 438: Find All Anagrams in a String - Character frequency matching
- LC 567: Permutation in String - Sliding window with frequency
Key Takeaways
- Bit Manipulation: Efficient way to track seen-once vs seen-multiple using XOR and AND operations
- Index Tracking: Store first occurrence index to find minimum later
- Two-Pass Approach: Count first, then find first unique
- Early Exit: Can optimize by scanning string in second pass instead of all 26 characters
References
- LC 387: First Unique Character in a String on LeetCode
- LeetCode Discuss — LC 387: First Unique Character in a String
- LeetCode Editorial (may require premium)