Reconstruct Digit Sequence from a Jumbled Letter Pool
StringsAuto-graded · 24 test casesLast updated
Tests whether a candidate can decode digit counts from a scrambled multiset of English number-word letters using systematic letter-frequency elimination.
Reconstruct Digit Sequence from a Jumbled Letter Pool
You receive a single string that is a random shuffling of all the letters from some collection of English digit words. The digit words are:
0 → zero 1 → one 2 → two 3 → three 4 → four
5 → five 6 → six 7 → seven 8 → eight 9 → nine
Every letter in the input came from exactly one of those words, but the word boundaries and the original letter order have been completely discarded. Your task is to figure out how many times each digit appears and return those digits as a string in non-decreasing order, one character per digit.
Implement:
def decode_digit_pool(letter_pool: str) -> str:
What the function receives and returns
letter_pool— a string of lowercase ASCII letters, guaranteed to be a valid shuffling of zero or more complete digit-word copies.- Return value — a string of digit characters
'0'–'9'sorted in ascending order, with each digit repeated as many times as it was found (e.g. three zeros and one two →"0002").
Important details
- The input is not a sequence of whole words pasted together; letter positions carry no information. Only letter frequencies matter.
- An empty input string means no digits were present; return
"". - The input can be up to 100 000 characters long. Aim for a solution that runs in O(n + k) time, where
nis the input length andkis the output length, using only a constant amount of counting state beyond the output itself.
Constraints
0 ≤ len(letter_pool) ≤ 100 000- Every character in
letter_poolis a lowercase ASCII letter ('a'–'z'). - The input is always a valid multiset of complete digit-word letters; no letter will be left over and no count will go negative.
- Digits may repeat any number of times (including zero occurrences of a particular digit).
- Output digits must appear in non-decreasing order (
'0' ≤ '1' ≤ … ≤ '9').
Examples
Example 1
Input: decode_digit_pool('owoztneoer')
Output: '012'
Example 2
Input: decode_digit_pool('fviefuro')
Output: '45'
Example 3
Input: decode_digit_pool('')
Output: ''
Example 4
Input: decode_digit_pool('zerozeroonefive')
Output: '0015'
Hints
Hint 1
Letter positions are meaningless — build a frequency count of the 26 letters and think entirely in terms of those counts. Which letters appear in only a single digit word?
Hint 2
Five letters each belong to exactly one digit word: z→zero, w→two, u→four, x→six, g→eight. Read those counts first and subtract each word's letters from the pool. After that step, what letters become newly unique?
Hint 3
Once 0, 2, 4, 6, and 8 are removed, five more letters each point to exactly one remaining word: o→one, t→three, f→five, s→seven, i→nine. Process them in that order, subtracting each word's contribution after counting it.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Why letter positions don't matter
All words have been torn apart and mixed together, so character positions carry zero information. The only thing preserved is the frequency of each letter across the entire input. Each digit word contributes a fixed set of letters; the total letter-count vector is the sum of all those contributions. The problem reduces to figuring out how many times each word was included.
Finding a unique elimination order
If a letter appears in only one digit word, its count directly reveals how many times that word was used. Five such letters exist in English spelling:
| Letter | Unique to |
|---|---|
z | zero |
w | two |
u | four |
x | six |
g | eight |
Read each marker's count, record it as that digit's tally, then subtract those word-letter counts from the pool. After this first pass, the pool contains letters only from the remaining five words (one, three, five, seven, nine). In that reduced pool, five more letters each appear in exactly one word:
| Letter | Unique to (after first pass) |
|---|---|
o | one (zero, two, four are gone) |
t | three (two, eight are gone) |
f | five (four is gone) |
s | seven (six is gone) |
i | nine (five, six, eight are gone) |
Process them the same way. The full order is: 0, 2, 4, 6, 8, 1, 3, 5, 7, 9.
Why this is correct
This is a triangular elimination. When we reach digit d, every other word that shared d's marker letter has already been fully counted and subtracted from the pool. So the remaining count of that marker equals exactly the number of times word d appears. By induction over the processing order, every count is exact. Because the input is guaranteed valid, no count ever goes negative, and the reconstruction is unique.
A common mistake is reaching for a marker before its uniqueness is established — for instance, using o for "one" before removing "zero", "two", and "four" would overcount.
Building the output
Once all ten tallies are known, construct the output by iterating digits 0 through 9 in order and emitting each digit as many times as its tally. Using str(d) * count keeps output construction O(k).
Complexity
- Time: O(n) to count letters, plus O(1) for the fixed 10-word elimination loop (at most 5 letters per word × 10 words), plus O(k) to build the output. Overall O(n + k).
- Space: O(1) counting state — at most 26 letter counts and 10 digit tallies — plus O(k) for the returned string.
Edge cases
- Empty input: all counts are zero, every tally is zero, output is
"". - A single digit repeated many times (e.g. all
eights): onlyg's count is nonzero; all other tallies emerge as zero naturally. - A mix skipping some digits entirely: unneeded markers simply read zero and subtract nothing.
Reference solution
from collections import Counter
def decode_digit_pool(letter_pool: str) -> str:
counts = Counter(letter_pool)
found = [0] * 10
# Processing order chosen so that the marker letter is unique among
# all words not yet subtracted from the pool at that step.
# First pass: letters that are globally unique to one word.
# Second pass: letters that become unique after the first pass.
order = [
(0, 'z', 'zero'), # 'z' only in zero
(2, 'w', 'two'), # 'w' only in two
(4, 'u', 'four'), # 'u' only in four
(6, 'x', 'six'), # 'x' only in six
(8, 'g', 'eight'), # 'g' only in eight
(1, 'o', 'one'), # 'o' now only in one (zero/two/four removed)
(3, 't', 'three'), # 't' now only in three (two/eight removed)
(5, 'f', 'five'), # 'f' now only in five (four removed)
(7, 's', 'seven'), # 's' now only in seven (six removed)
(9, 'i', 'nine'), # 'i' now only in nine (five/six/eight removed)
]
for digit, marker, word in order:
amount = counts[marker]
found[digit] = amount
for ch in word:
counts[ch] -= amount
return ''.join(str(digit) * found[digit] for digit in range(10))