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 <= 105
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.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to solve this puzzle is to try every possible combination of digits, represented by their English spellings, to see if they match the input string. This means trying to construct the original digits, one by one, and checking if we can form the input.
Here's how the algorithm would work step-by-step:
def reconstruct_original_digits_brute_force(input_string):
all_digits = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
digit_counts = [0] * 10
best_solution = None
def backtrack(remaining_string, current_digit_counts, digit_index):
nonlocal best_solution
#If all digits have been considered
if digit_index == 10:
if not remaining_string:
current_solution = "".join([str(i) * current_digit_counts[i] for i in range(10)])
if best_solution is None or current_solution < best_solution:
best_solution = current_solution
return
else:
return
# Try different counts for the current digit
for count in range(11):
new_remaining_string = remaining_string
valid_path = True
#Subtract the letters used to spell out that many zeros from the input string
for _ in range(count):
for char in all_digits[digit_index]:
if char in new_remaining_string:
new_remaining_string = new_remaining_string.replace(char, '', 1)
else:
valid_path = False
break
if not valid_path:
break
if valid_path:
current_digit_counts[digit_index] = count
# Recursive call to the next digit with the updated string
backtrack(new_remaining_string, current_digit_counts[:], digit_index + 1)
backtrack(input_string, digit_counts, 0)
return best_solution if best_solution else ''
The problem involves figuring out which digits are hidden within a jumbled word made from spelling out those digits. The efficient approach is to count the letters in the jumbled word and then deduce the digits based on unique letter patterns. We strategically identify digits that have unique letter occurrences to count them directly.
Here's how the algorithm would work step-by-step:
def reconstruct_original_digits(input_string):
letter_counts = {}
for char in input_string:
letter_counts[char] = letter_counts.get(char, 0) + 1
digit_counts = [0] * 10
# Count digits based on unique letters.
digit_counts[0] = letter_counts.get('z', 0)
digit_counts[2] = letter_counts.get('w', 0)
digit_counts[4] = letter_counts.get('u', 0)
digit_counts[6] = letter_counts.get('x', 0)
digit_counts[8] = letter_counts.get('g', 0)
# Deduce remaining digits after handling unique letters.
digit_counts[3] = letter_counts.get('h', 0) - digit_counts[8]
digit_counts[1] = letter_counts.get('o', 0) - digit_counts[0] - digit_counts[2] - digit_counts[4]
digit_counts[5] = letter_counts.get('f', 0) - digit_counts[4]
digit_counts[7] = letter_counts.get('v', 0) - digit_counts[5]
# 'i' appears in five, six, eight, nine. Determine nine last
digit_counts[9] = letter_counts.get('i', 0) - digit_counts[5] - digit_counts[6] - digit_counts[8]
result = ''
for digit in range(10):
result += str(digit) * digit_counts[digit]
return result
Case | How to Handle |
---|---|
Null or empty input string | Return an empty list or null, based on problem specifications, since there are no digits to reconstruct. |
Input string contains characters other than lowercase English letters | Reject the input or preprocess by removing/ignoring non-alphabetic characters to avoid incorrect counts. |
Input string is extremely long (close to the maximum allowable string length) | Ensure the character counting and digit reconstruction processes are efficient enough to handle large inputs without exceeding time limits. |
Input string contains letters that don't form any valid digit words | Return an empty list or a predefined error value, indicating no valid digit reconstruction is possible. |
Input string contains a combination of letters that could lead to multiple valid digit sequences | The algorithm should deterministically choose one valid sequence based on a predefined priority (e.g., always prioritizing certain digit words). |
The frequency counts of certain letters are zero or negative after processing some digits | Handle the edge case gracefully, by either detecting the error and returning an empty list or by continuing the algorithm ignoring the negative/zero values as they imply invalid cases. |
The reconstructed digits, when concatenated, result in leading zeros (e.g., 'zero zero') | Ensure the resulting list is properly formatted by stripping leading zeros if required, depending on problem specifications. |
Integer overflow when counting character frequencies or digit occurrences. | Use appropriate data types (e.g., long) for storing frequency counts to prevent potential integer overflows. |