423. Reconstruct Original Digits from English - Detailed Explanation

Problem Statement

You’re given a non‑empty string s containing an out‑of‑order English representation of digits zero through nine. Each digit’s English word is concatenated, jumbled, and mixed with the others. Your task is to recover the original digits in ascending order.

Examples
1.

Input: s = "owoztneoer"
Output: "012"
Explanation: The letters can be rearranged to "onezero two", so the digits are [0,1,2].
Input: s = "fviefuro"
Output: "45"
Explanation: The letters can be rearranged to "five four", so the digits are [4,5].
Input: s = "zerozero"
Output: "00"
Explanation: Two copies of "zero".

Constraints

  • 1 ≤ s.length ≤ 10⁵
  • s consists only of lowercase English letters.
  • It is guaranteed that s is a valid jumble of one or more digit words.

Hints

  1. Which English digit names contain a letter no other digit word has?
  2. After you count those “unique” letters, remove their contribution and look for the next set of hints.
  3. You only need to tally letter frequencies once and do a constant‑time deduction for each digit.

Approach 1: Character‑Count with Unique Identifiers

This is the standard O(n) solution using the fact that some digit names have letters nobody else uses.

  1. Build a frequency map of all letters in s.
  2. Detect digits with truly unique letters:
    • 'z'0 (“zero”)
    • 'w'2 (“two”)
    • 'u'4 (“four”)
    • 'x'6 (“six”)
    • 'g'8 (“eight”)
  3. Subtract those counts from the global map (conceptually), then detect the next set:
    • 'h' appears only in 3 (“three”) and 8; so count3 = freq['h'] − count8
    • 'f' appears only in 5 (“five”) and 4; so count5 = freq['f'] − count4
    • 's' appears only in 7 (“seven”) and 6; so count7 = freq['s'] − count6
  4. Finally the remaining digits share more common letters:
    • 'o' appears in 0,1,2,4 → count1 = freq['o'] − count0 − count2 − count4
    • 'i' appears in 5,6,8,9 → count9 = freq['i'] − count5 − count6 − count8
  5. Construct the output string by appending each digit d exactly countd times, in ascending order from 0 to 9.

Step‑by‑Step Example

Take s = "fviefuro":

  • Frequency map: f:2,v:1,i:1,e:1,u:1,r:1,o:1
  • Unique letters:
    • count4 = freq['u'] = 1
    • count2 = freq['w'] = 0
    • count0 = freq['z'] = 0
    • count6 = freq['x'] = 0
    • count8 = freq['g'] = 0
  • Next:
    • count5 = freq['f'] − count4 = 2−1 = 1
    • count3 = freq['h'] − count8 = 0−0 = 0
    • count7 = freq['s'] − count6 = 0−0 = 0
  • Last:
    • count1 = freq['o'] − count0 − count2 − count4 = 1−0−0−1 = 0
    • count9 = freq['i'] − count5 − count6 − count8 = 1−1−0−0 = 0
  • Only non‑zero counts are count4=1 and count5=1 → output “45”.

Complexity Analysis

  • Time: O(n) to count letters, plus O(1) work per digit (constant steps).
  • Space: O(1) extra (just a 26‑entry array for frequencies and a 10‑entry array for digit counts).

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Approach 2: Brute‑Force Removal (Inefficient)

You could try repeatedly matching every digit word against the remaining letters, removing matched letters one digit at a time. This ends up O(n·D) or worse (D = number of digit names) and can degrade to O(n²) in practice. It’s fine for tiny inputs but fails at 10⁵ length.

Common Mistakes

  • Counting overlapping letters too early (e.g. using 'h' before removing eight).
  • Forgetting to subtract previously counted digits when computing shared letters.
  • Building the final string in the wrong order, giving unsorted results.

Edge Cases

  • Input has multiple copies of the same digit (e.g. "zerozerozero""000").
  • All digits are those with unique identifiers (e.g. only zeros and twos).
  • Very long strings that stress performance—only the O(n) method passes.

Alternative Variations

  • Reconstructing words from a letter‑jumble in other languages.
  • Given the output digits, produce the lexicographically smallest original string of words.
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
197. Rising Temperature - Detailed Explanation
Learn to solve Leetcode 197. Rising Temperature with multiple approaches.
1760. Minimum Limit of Balls in a Bag - Detailed Explanation
Learn to solve Leetcode 1760. Minimum Limit of Balls in a Bag with multiple approaches.
1017. Convert to Base -2 - Detailed Explanation
Learn to solve Leetcode 1017. Convert to Base -2 with multiple approaches.
486. Predict the Winner - Detailed Explanation
Learn to solve Leetcode 486. Predict the Winner with multiple approaches.
523. Continuous Subarray Sum - Detailed Explanation
Learn to solve Leetcode 523. Continuous Subarray Sum with multiple approaches.
2337. Move Pieces to Obtain a String - Detailed Explanation
Learn to solve Leetcode 2337. Move Pieces to Obtain a String with multiple approaches.
Related Courses
Course image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
Discounted price for Your Region

$197

Course image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$78

Course image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Image
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.