KatInterview

423. Reconstruct Original Digits from English

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method: Counting with Unique Character Identification

The core of this problem is to determine the frequency of each digit from 0 to 9 based on the characters in the input string s. A simple frequency count of letters is challenging because many letters are shared between the English words for different digits (e.g., 'o' is in "one", "two", "four", "zero").

Solution

Reconstruct Original Digits from English in Python

from collections import Counter

class Solution:
    def originalDigits(self, s: str) -> str:
        # Step 1: Count character frequencies
        counts = Counter(s)
        
        # Step 2: Create a list to store digit counts
        digits = [0] * 10
        
        # Step 3: Determine digit counts based on unique characters and deductions
        # The order of these calculations is important.
        
        # Digits with unique identifying characters
        digits[0] = counts['z']  # 'z' is unique to "zero"
        digits[2] = counts['w']  # 'w' is unique to "two"
        digits[4] = counts['u']  # 'u' is unique to "four"
        digits[6] = counts['x']  # 'x' is unique to "six"
        digits[8] = counts['g']  # 'g' is unique to "eight"
        
        # Deduce counts for other digits
        digits[3] = counts['h'] - digits[8]         # 'h' is in "three" and "eight"
        digits[5] = counts['f'] - digits[4]         # 'f' is in "five" and "four"
        digits[7] = counts['s'] - digits[6]         # 's' is in "seven" and "six"
        
        # Further deductions based on previously found counts
        digits[1] = counts['o'] - digits[0] - digits[2] - digits[4] # 'o' in "one", "zero", "two", "four"
        digits[9] = counts['i'] - digits[5] - digits[6] - digits[8] # 'i' in "nine", "five", "six", "eight"
        
        # Step 4: Build the result string in ascending order
        result = []
        for i in range(10):
            result.append(str(i) * digits[i])
        
        return "".join(result)

Reconstruct Original Digits from English in Java

class Solution {
    public String originalDigits(String s) {
        // Step 1: Count character frequencies
        int[] counts = new int[26];
        for (char c : s.toCharArray()) {
            counts[c - 'a']++;
        }

        // Step 2: Create an array to store digit counts
        int[] digits = new int[10];

        // Step 3: Determine digit counts based on unique characters and deductions
        // The order of these calculations is important.

        // Digits with unique identifying characters
        digits[0] = counts['z' - 'a']; // 'z' is unique to "zero"
        digits[2] = counts['w' - 'a']; // 'w' is unique to "two"
        digits[4] = counts['u' - 'a']; // 'u' is unique to "four"
        digits[6] = counts['x' - 'a']; // 'x' is unique to "six"
        digits[8] = counts['g' - 'a']; // 'g' is unique to "eight"

        // Deduce counts for other digits
        digits[3] = counts['h' - 'a'] - digits[8];          // 'h' is in "three" and "eight"
        digits[5] = counts['f' - 'a'] - digits[4];          // 'f' is in "five" and "four"
        digits[7] = counts['s' - 'a'] - digits[6];          // 's' is in "seven" and "six"
        
        // Further deductions based on previously found counts
        digits[1] = counts['o' - 'a'] - digits[0] - digits[2] - digits[4]; // 'o' in "one", "zero", "two", "four"
        digits[9] = counts['i' - 'a'] - digits[5] - digits[6] - digits[8]; // 'i' in "nine", "five", "six", "eight"

        // Step 4: Build the result string in ascending order
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < digits[i]; j++) {
                sb.append(i);
            }
        }
        
        return sb.toString();
    }
}

Reconstruct Original Digits from English in C++

class Solution {
public:
    string originalDigits(string s) {
        // Step 1: Count character frequencies
        vector<int> counts(26, 0);
        for (char c : s) {
            counts[c - 'a']++;
        }

        // Step 2: Create an array to store digit counts
        vector<int> digits(10, 0);

        // Step 3: Determine digit counts based on unique characters and deductions
        // The order of these calculations is important.

        // Digits with unique identifying characters
        digits[0] = counts['z' - 'a']; // 'z' is unique to "zero"
        digits[2] = counts['w' - 'a']; // 'w' is unique to "two"
        digits[4] = counts['u' - 'a']; // 'u' is unique to "four"
        digits[6] = counts['x' - 'a']; // 'x' is unique to "six"
        digits[8] = counts['g' - 'a']; // 'g' is unique to "eight"

        // Deduce counts for other digits
        digits[3] = counts['h' - 'a'] - digits[8];          // 'h' is in "three" and "eight"
        digits[5] = counts['f' - 'a'] - digits[4];          // 'f' is in "five" and "four"
        digits[7] = counts['s' - 'a'] - digits[6];          // 's' is in "seven" and "six"
        
        // Further deductions based on previously found counts
        digits[1] = counts['o' - 'a'] - digits[0] - digits[2] - digits[4]; // 'o' in "one", "zero", "two", "four"
        digits[9] = counts['i' - 'a'] - digits[5] - digits[6] - digits[8]; // 'i' in "nine", "five", "six", "eight"
        
        // Step 4: Build the result string in ascending order
        string result = "";
        for (int i = 0; i < 10; ++i) {
            result += string(digits[i], '0' + i);
        }
        
        return result;
    }
};

Reconstruct Original Digits from English in TypeScript

function originalDigits(s: string): string {
    // Step 1: Count character frequencies
    const counts: { [key: string]: number } = {};
    for (const char of s) {
        counts[char] = (counts[char] || 0) + 1;
    }

    const getCount = (char: string): number => counts[char] || 0;

    // Step 2: Create an array to store digit counts
    const digits = new Array(10).fill(0);

    // Step 3: Determine digit counts based on unique characters and deductions
    // The order of these calculations is important.

    // Digits with unique identifying characters
    digits[0] = getCount('z'); // 'z' is unique to "zero"
    digits[2] = getCount('w'); // 'w' is unique to "two"
    digits[4] = getCount('u'); // 'u' is unique to "four"
    digits[6] = getCount('x'); // 'x' is unique to "six"
    digits[8] = getCount('g'); // 'g' is unique to "eight"

    // Deduce counts for other digits
    digits[3] = getCount('h') - digits[8];         // 'h' is in "three" and "eight"
    digits[5] = getCount('f') - digits[4];         // 'f' is in "five" and "four"
    digits[7] = getCount('s') - digits[6];         // 's' is in "seven" and "six"

    // Further deductions based on previously found counts
    digits[1] = getCount('o') - digits[0] - digits[2] - digits[4]; // 'o' in "one", "zero", "two", "four"
    digits[9] = getCount('i') - digits[5] - digits[6] - digits[8]; // 'i' in "nine", "five", "six", "eight"

    // Step 4: Build the result string in ascending order
    let result = '';
    for (let i = 0; i < 10; i++) {
        result += String(i).repeat(digits[i]);
    }

    return result;
}

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.