Find the First Character Appearing Exactly Once
Arrays & HashingAuto-graded · 17 test casesLast updated
Tests the ability to use hash counting (frequency maps) to efficiently locate the first character in a string that appears exactly once.
First Unique Character in a Stream
You are given a string message representing a sequence of lowercase English letters received one by one (left to right). Your task is to find the index of the first character that appears exactly once in the entire string.
If no such character exists, return -1.
Important: You must consider the full string when determining character frequencies — a character is "unique" only if its total count across the entire string is exactly 1. Then, among all such unique characters, return the index of the one that appears earliest (leftmost) in the string.
Constraints
0 <= len(message) <= 2000messageconsists only of lowercase English letters ('a'–'z').- If
messageis empty, return-1. - If multiple characters each appear exactly once, return the smallest index among them.
- If no character appears exactly once, return
-1.
Examples
Example 1
Input: first_unique_char_index('interview')
Output: 1
Example 2
Input: first_unique_char_index('aabbcc')
Output: -1
Example 3
Input: first_unique_char_index('abacabad')
Output: 3
Hints
Hint 1
Count the frequency of every character in the string using a dictionary or array of size 26 before scanning for the answer.
Hint 2
Once you have the frequency map, do a second pass from left to right and return the index of the first character whose count equals 1.
Hint 3
Think about what happens when all characters repeat — make sure your code returns -1 in that case rather than an invalid index.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem has a natural two-pass structure enabled by hash counting:
Pass 1 — Build a frequency map:
Iterate over the entire string once and count how many times each character appears. Python's collections.Counter does this in one line, but a plain dict or a length-26 list works equally well.
Pass 2 — Find the first unique:
Iterate over the string from left to right. The first index i where freq[message[i]] == 1 is the answer. If the loop completes without finding such an index, return -1.
Why it is correct:
- We examine the full frequency before scanning, so a character that appears later in the string correctly disqualifies an earlier occurrence.
- The left-to-right scan guarantees we pick the earliest unique character.
- Edge cases (empty string, all duplicates, single character, all unique) are handled without special branches beyond the initial empty check.
Complexity:
- Time: O(n) — two linear passes over a string of length n.
- Space: O(1) — the frequency map holds at most 26 entries (bounded alphabet), so it is effectively constant space.
from collections import Counter
def first_unique_char_index(message: str) -> int:
if not message:
return -1
freq = Counter(message)
for i, ch in enumerate(message):
if freq[ch] == 1:
return i
return -1