First Unique Character in a String
Two-Pass Hashing / Frequency CountSolved Nov 7, 2025
Asked at 12 companies
9 recent
- Goldman Sachs
- EPAM Systems
- tcs
- Bloomberg
- Amazon
- Microsoft
- Yandex
- Meta
- Morgan Stanley
+2 moreShow fewer
- Fidelity
- Walmart Labs
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven a string S, find the first non-repeating character in it and return its index. If it does not exist, return -1.ReadHide
Problem Description
Given a string S, find the first non-repeating character in it and return its index. If it does not exist, return -1.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Use a two-pass approach: 1) Count frequencies in a Hash Map. 2) Iterate through the string again and return the index of the first character with a count of 1.
Approaches
1. Two-Pass Hashing (Optimal) - O(N) ⭐
Idea: This is the optimal time complexity solution. You need two passes: the first to gather all frequency data, and the second to check the original indices. Since the character set (a-z) is small and constant, the space complexity is considered O(1).
- Pass 1 (Count): Use a fixed-size array (or Hash Map) of size 26 to store the frequency of every character.
O(N). - Pass 2 (Find): Iterate through the original string again. The first character you encounter whose count is exactly 1 is the answer. Return its index.
O(N).
2. Brute Force - O(N²)
Idea: For every character S[i], iterate through the entire rest of the string (S[j] where j ne i) to see if S[i] is repeated.
Time: O(N²).
Solution code
import sys
from collections import Counter
class Solution:
# Optimized: O(N) Time, O(1) Space (Using Counter and a two-pass strategy)
def firstUniqChar_optimal(self, s: str) -> int:
if not s: return -1
# Pass 1: Count frequencies (O(N))
counts = Counter(s)
# Pass 2: Find the first index (O(N))
for i, char in enumerate(s):
if counts[char] == 1:
return i
return -1
# Brute Force: O(N^2) Time
def firstUniqChar_brute(self, s: str) -> int:
N = len(s)
for i in range(N):
is_unique = True
for j in range(N):
if i != j and s[i] == s[j]:
is_unique = False
break
if is_unique:
return i
return -1
def solve(self):
try:
s = sys.stdin.readline().strip()
except:
return
result = self.firstUniqChar_optimal(s)
print(result)
if __name__ == "__main__":
Solution().solve()Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language