Reconstruct Original Digits from English - Complete Solution Guide
Reconstruct Original Digits from English is LeetCode problem 423, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a string s containing an out-of-order English representation of digits 0-9 , return the digits in ascending order . Example 1: Input: s = "owoztneoer" Output: "012" Example 2: Input: s = "fviefuro" Output: "45" Constraints: 1 <= s.length <= 10 5 s[i] is one of the characters ["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"] . s is guaranteed to be valid.
Detailed Explanation
The problem asks us to reconstruct the original digits (0-9) from a given string `s`. The string `s` contains an out-of-order English representation of these digits. We need to return the digits in ascending order. The input string `s` is guaranteed to be valid, meaning it will always contain a valid combination of letters that can form digits from zero to nine. The constraints also tell us the string length and the allowed characters.
Solution Approach
The solution uses a counting approach. It first counts the frequency of each character in the input string. Then, it uses the unique characteristics of the English spellings of digits to determine the count of each digit. By identifying unique letters in certain digit names, we can calculate the counts of those digits directly. Then we subtract counts of digits with shared letters from the remaining letters to deduce the remaining digit counts. Finally, we construct the output string by appending each digit according to its calculated frequency and ordering it numerically.
Step-by-Step Algorithm
- Step 1: Initialize a character count map (or array) to store the frequencies of each character in the input string `s`.
- Step 2: Initialize an array `digit_counts` of size 10 to store the frequency of each digit (0-9).
- Step 3: Identify and count digits with unique letters: zero ('z'), two ('w'), four ('u'), six ('x'), eight ('g'). Update the `digit_counts` array accordingly.
- Step 4: Calculate the counts of the remaining digits by subtracting the counts of the already identified digits that share letters: three ('h' - 'eight'), five ('f' - 'four'), seven ('s' - 'six').
- Step 5: Calculate the counts of the remaining digits: one ('o' - 'zero' - 'two' - 'four'), nine ('i' - 'five' - 'six' - 'eight').
- Step 6: Build the resulting string by iterating through the `digit_counts` array. For each digit, append it to the result string as many times as its frequency indicates.
- Step 7: Return the resulting string.
Key Insights
- Insight 1: Recognize unique letters that identify certain digits. For instance, 'z' is unique to 'zero', 'w' is unique to 'two', 'u' is unique to 'four', 'x' is unique to 'six', and 'g' is unique to 'eight'.
- Insight 2: After identifying the counts of digits using unique letters, use the remaining letters to deduce counts for the other digits. Subtract the counts of digits with shared letters.
- Insight 3: Use a consistent order of deduction to avoid circular dependencies and incorrect counts. For example, calculate the counts of digits 0, 2, 4, 6, and 8 first because they have unique characters. After that derive 3, 5, and 7. Then finally find digits 1 and 9
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Hash Table, Math, String.
Companies
Asked at: NetApp, Salesforce, Wix.