Reconstruct Original Digits from English - Problem

Given a string s containing an out-of-order English representation of digits 0-9, return the digits in ascending order.

The string contains English words for digits like "zero", "one", "two", etc., but the letters are jumbled together. Your task is to figure out which digits are present and return them sorted.

Note: Each digit appears at most once in the input string.

Input & Output

Example 1 — Basic Mixed Digits
$ Input: s = "owtzero"
Output: "02"
💡 Note: The string contains letters for "zero" (z,e,r,o) and "two" (t,w,o). Since we need ascending order, return "02".
Example 2 — Single Digit
$ Input: s = "fviefuro"
Output: "45"
💡 Note: Contains "four" (f,o,u,r) and "five" (f,i,v,e). The shared 'f' appears twice. Result is "45".
Example 3 — Multiple Overlapping
$ Input: s = "nnei"
Output: "19"
💡 Note: Contains "one" (o,n,e) and "nine" (n,i,n,e). Both share 'n' and 'e', but the counts work out perfectly for digits 1 and 9.

Constraints

  • 1 ≤ s.length ≤ 105
  • s consists of lowercase English letters only
  • s contains an out-of-order representation of some digits 0-9
  • Each digit appears at most once

Visualization

Tap to expand
Reconstruct Digits: "owtzero" → "02"Scrambled Input"owtzero"Mixed letters from digit wordsLetter Analysisz→0 (zero), w→2 (two)Unique letter fingerprints"zero"0z,e,r,o"two"2t,w,oResult: "02" (ascending order)
Understanding the Visualization
1
Input
Scrambled letters from English digit words
2
Process
Use letter frequency analysis to identify digits
3
Output
Digits in ascending order
Key Takeaway
🎯 Key Insight: Use unique letters as fingerprints - some digits have letters that appear in no other digit word
Asked in
Google 23 Facebook 18 Microsoft 15
31.5K Views
Medium Frequency
~25 min Avg. Time
892 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