Recover Sorted Digits from Scrambled English Number Words
MicrosoftSoftware Engineer · OnsiteLast updated
Recover sorted digits from scrambled English number-word letters, including repeats, and analyze why arbitrary customer vocabularies may lose unique recoverability.
A string contains a shuffled collection of letters from the English digit names zero, one, two, three, four, five, six, seven, eight, and nine. Recover the digits and return them in ascending order, preserving repetitions.
Implement recover_digits(letters: string) -> string.
Constraints & Assumptions
- Input has at most 100000 lowercase ASCII letters and is guaranteed to be a valid multiset of complete English digit names. The empty input represents no digits.
- Word boundaries and letter order have been lost. This is not substring segmentation of concatenated intact words.
- Output contains only
0through9, sorted nondecreasingly, with one digit per recovered word. - Aim for O(input length + output length) time and constant-sized counting state, apart from output.
Examples
letters = "owoztneoer"
result = "012"
letters = "fviefuro"
result = "45"
The reported follow-up replaces English digit names with arbitrary customer-provided words for digits 0–9. Explain whether your reasoning still works, what would establish unique recoverability, and how you would detect or report ambiguous mappings. Do not assume arbitrary words contain a distinguishing letter. For example, assigning word a to one digit and word aa to another can make the same letter multiset represent different digit sequences.
Hints
Hint 1
Use structure specific to the fixed vocabulary: Some letters initially occur in only one digit name. After removing those words' contributions, other letters can become distinguishing.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.