KatInterview

Reconstruct Digit Sequence from a Jumbled Letter Pool

StringsAuto-graded · 24 test cases
MediumAuto-gradedStringsstringshash-mapgreedy-intervals

Last 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 n is the input length and k is 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_pool is 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.