Welcome to Day 51 of the #80DaysOfChallenges journey! This intermediate challenge cracks the popular first non-repeating character problem (LeetCode #387 style), scanning a string to find and return the very first character that appears only once, or None if every char repeats. The solution uses a dictionary frequency map in two smart passes for O(n) time and crystal-clear logic that's a favorite in coding interviews.
Example:
"leetcode" → 'l'
"loveleetcode" → 'v'
"aabb" → None
If you're grinding LeetCode, prepping for FAANG interviews, or building features like unique username validation, this "Python first unique character" solution is clean, efficient, and the exact pattern recruiters love.
💡 Key Takeaways from Day 51: First Unique Char Function
This task delivers a textbook two-pass hash map approach: count frequencies first, then scan in order to find the earliest unique. We'll break it down: freq dict build in first pass, second pass for position-aware return, and main with clean output.
1. First Pass: Build Frequency Map
def first_unique_char(s: str):
"""
Return the first non-repeating character in the given string.
Uses a frequency map to count occurrences, then scans again
to find the first character with count equal to 1.
"""
freq = {}
for ch in s:
freq[ch] = freq.get(ch, 0) + 1
Classic get() idiom for counting: default 0 if new, +1 otherwise. First pass builds complete frequency profile in O(n).
2. Second Pass: Return First with Count == 1
for ch in s:
if freq[ch] == 1:
return ch
return None
Crucial second pass in original order: guarantees earliest position. Returns immediately on first unique. For "leetcode": freq={'l':1,'e':3,'t':2,'c':1,'o':1,'d':1}, returns 'l' first.
Handles edges perfectly: empty string → None, all duplicates → None, single char → that char.
3. Main Interactive: Simple Input and Output
user_input = input("Enter a string: ").strip()
result = first_unique_char(user_input)
if result is None:
print("No unique character found.")
else:
print("First non-repeating character:", result)
Clean prompt, strip whitespace, clear result message. Ready for testing.
🎯 Summary and Reflections
This first unique character solution is elegant, performant, and interview-gold. It reinforced:
- Two-pass hash map pattern: Count first, decide second, preserves order.
- get() counting idiom: Pythonic and clean.
- Early return: Stop as soon as answer found.
This exact technique appears in countless string/hash problems. For lowercase English only, swap dict for array[26] to hit O(1) space.
Advanced Alternatives:
- collections.Counter(s) for freq
- Single pass with OrderedDict or list+dict for index tracking
- return s.index(ch) for ch in s if freq[ch]==1 (clever but slower)
But this version wins for clarity and speed.
🚀 Next Steps and Resources
Day 51 delivered hash map mastery for strings. Try "aabccdeef" → 'b'
- Source Code for Challenge #51: scripts/first_unique_char.py
- Main Repository: 80-days-of-challenges
- Daily Updates: Twitter/X (@Shahrouzlogs)
What's the trickiest string you found the first unique in? Drop it below! 🔥
Top comments (0)