Earliest Non-Repeating Symbol Position
StringsAuto-graded · 23 test casesLast updated
Locate the index of the earliest symbol in an alphanumeric string that occurs only once, using frequency counting and an ordered scan, or return -1.
You are given a string text made up of uppercase letters, lowercase letters, and digits.
Find the symbol that appears exactly one time in the entire string and sits at the lowest index among all such symbols. Return that index (zero-based). If every symbol in the string occurs more than once, or the string is empty, return -1.
Rules to follow precisely:
- Matching is case-sensitive:
'a'and'A'are two different symbols. - Digits are treated like any other symbol.
- "Earliest" refers to position in the original string, not alphabetical order.
- Return the index, not the symbol itself.
Your solution should run in linear time in the length of the string.
Constraints
textcontains only characters froma-z,A-Z, and0-9.0 <= len(text) <= 10^5- The function returns an integer: a valid index in
text, or-1.
Examples
Example 1
Input: earliest_single_occurrence('leetcode')
Output: 0
Example 2
Input: earliest_single_occurrence('loveleetcode')
Output: 2
Example 3
Input: earliest_single_occurrence('aA1bB2a')
Output: 1
Hints
Hint 1
Whether a symbol is unique depends on the whole string, so looking at it once from left to right is not enough on its own.
Hint 2
First tally how many times every symbol occurs. Then walk through the string again in its original order and check each symbol's tally.
Hint 3
Iterate over the string itself in the second pass (not over the tally table) so the first match is guaranteed to be the one with the smallest index.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Whether a symbol is unique can only be decided after the whole string has been seen, so use two passes:
- Count pass: build a frequency table that maps each symbol to the number of times it occurs in
text. - Scan pass: walk through
textfrom index 0 upward. Return the first index whose symbol has a count of exactly 1. If the loop ends with no match, return-1.
Why it is correct
- After the first pass, the table holds the exact total count for every symbol, because each position contributes one increment.
- The second pass inspects indices in increasing order, so the first index that satisfies the condition is by definition the smallest index of a symbol occurring once.
- If no index satisfies it, no symbol is unique, so
-1is the right answer. The empty string never enters the loop and also yields-1. - Iterating over the string rather than the table is what ties the result to original positions. Case is preserved because the raw characters are counted without any normalization.
Complexity
- Time: O(n), with two linear passes and constant-time average table operations.
- Space: O(k), where
kis the number of distinct symbols. Since the alphabet is at most 62 symbols, this is effectively O(1).
Sorting-based alternatives cost O(n log n) and lose the original ordering unless indices are carried along, so counting is the better choice here.
Reference solution
from collections import Counter
def earliest_single_occurrence(text: str) -> int:
freq = Counter(text)
for i, ch in enumerate(text):
if freq[ch] == 1:
return i
return -1