Reconstruct Original Digits from English - Problem

You're given a jumbled string containing the English words for digits 0-9, but all mixed up! Your task is to figure out which digits were originally present and return them in ascending order.

For example, if you see "owzttnfour", this contains the letters for "two" and "four", so you should return "24".

The Challenge: Some letters appear in multiple digit words (like 'o' in both "zero" and "four"), so you need to be clever about how you count them!

Goal: Reconstruct the original digits from the scrambled English representation and return them sorted in ascending order as a string.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "owzttnfour"
โ€บ Output: "24"
๐Ÿ’ก Note: The string contains letters for "two" and "four". Since we return digits in ascending order, the answer is "24".
example_2.py โ€” Multiple Digits
$ Input: s = "fviefuro"
โ€บ Output: "45"
๐Ÿ’ก Note: The string contains letters for "four" and "five". Sorted in ascending order gives us "45".
example_3.py โ€” Single Digit
$ Input: s = "nnei"
โ€บ Output: "9"
๐Ÿ’ก Note: The string contains letters for "nine" only, so the answer is "9".

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s[i] is a lowercase English letter
  • s is guaranteed to be a valid representation of some digits 0-9

Visualization

Tap to expand
๐Ÿ” The Letter Detective's MethodScrambled: "owzttnfour"๐Ÿ•ต๏ธ Clue Analysis:โ€ข 'z' = smoking gun for ZEROโ€ข 'w' = smoking gun for TWO๐Ÿ”ฌ Evidence Board:DIGIT 0z: 1 foundCONFIRMED!DIGIT 2w: 1 foundCONFIRMED!DIGIT 4u: 2 foundCONFIRMED!๐Ÿ“‹ Letter Inventory:Available: o(2), w(1)โœ“, z(1)โœ“, t(2), n(2), f(1), u(2)โœ“, r(1)๐ŸŽฏ Deduction Process:1. Found: 0(1), 2(1), 4(2) using unique letters2. Check remaining letters for other patterns3. No other complete digit words can be formed๐ŸŽ‰ Case Closed!Result: "024"Smart detective work!The key is finding unique letter clues first, then deducing the rest!
Understanding the Visualization
1
Gather Evidence
Count how many times each letter appears in the scrambled text
2
Find Smoking Guns
Look for unique letters: 'z' only appears in 'zero', 'w' only in 'two', etc.
3
Solve Easy Cases
Count digits with unique letters first - these are guaranteed wins
4
Deduce Harder Cases
Use the remaining letters to figure out the trickier digits
5
Present Evidence
Arrange your findings in ascending order to present the solution
Key Takeaway
๐ŸŽฏ Key Insight: Look for unique letters first (z, w, u, x, g), then systematically deduce remaining digits. This transforms a complex combinatorial problem into a simple counting and deduction exercise!
Asked in
Google 42 Microsoft 38 Amazon 31 Apple 24
43.2K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen