Reconstruct Original Digits from English

This problem can be solved by recognizing unique letters that correspond to specific digits and then mapping them to the digits.

Here’s the implementation:

  1. Count the frequency of each letter in the given string.
  2. Identify unique characters that correspond to each digit.
  3. Based on the occurrence of these unique characters, find the count of each digit.
  4. Build the final result in ascending order.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
    def originalDigits(self, s: str) -> str:
        # Count the frequency of each character
        char_count = [0] * 26
        for char in s:
            char_count[ord(char) - ord('a')] += 1

        # Count the frequency of each digit based on unique characters
        digits_count = [0] * 10
        digits_count[0] = char_count[ord('z') - ord('a')]  # digit 0: "z"
        digits_count[2] = char_count[ord('w') - ord('a')]  # digit 2: "w"
        digits_count[4] = char_count[ord('u') - ord('a')]  # digit 4: "u"
        digits_count[6] = char_count[ord('x') - ord('a')]  # digit 6: "x"
        digits_count[8] = char_count[ord('g') - ord('a')]  # digit 8: "g"
        digits_count[1] = char_count[ord('o') - ord('a')] - digits_count[0] - digits_count[2] - digits_count[4] # digit 1: "o"
        digits_count[3] = char_count[ord('h') - ord('a')] - digits_count[8] # digit 3: "h"
        digits_count[5] = char_count[ord('f') - ord('a')] - digits_count[4] # digit 5: "f"
        digits_count[7] = char_count[ord('s') - ord('a')] - digits_count[6] # digit 7: "s"
        digits_count[9] = char_count[ord('i') - ord('a')] - digits_count[6] - digits_count[8] - digits_count[5] # digit 9: "i"

        # Build the result string in ascending order
        result = []
        for digit, count in enumerate(digits_count):
            result.append(str(digit) * count)
        return ''.join(result)

This code takes into account the constraints and leverages the characteristics of the English representation of digits to decode the original digits in ascending order.