Given a non-empty string containing an out-of-order English representation of digits 0-9, sort them in ascending order, and return the result as a string.
Let me know if any clarifications are needed for the problem. Assuming the input is a valid representation of the digits with only lowercase English letters.
To solve this problem, we will follow these steps:
def originalDigits(s: str) -> str:
from collections import Counter
# Step 1: Frequency count of each character in the input string
count = Counter(s)
# Step 2: Initialize the frequency array for digits 0-9
out = [0] * 10
# Step 3: Deduce counts from characters unique to each digit
out[0] = count['z']
out[2] = count['w']
out[4] = count['u']
out[6] = count['x']
out[8] = count['g']
# Step 4: Deduce counts of non-unique characters
out[1] = count['o'] - out[0] - out[2] - out[4]
out[3] = count['h'] - out[8]
out[5] = count['f'] - out[4]
out[7] = count['s'] - out[6]
out[9] = count['i'] - out[5] - out[6] - out[8]
# Step 5: Construct the output from the frequencies
result = []
for i in range(10):
result.append(str(i) * out[i])
return ''.join(result)
# Example usage
print(originalDigits("owoztneoer")) # Output: "012"
print(originalDigits("fviefuro")) # Output: "45"
The time complexity of the algorithm is O(n), where n is the length of the input string. This is because we are making a single pass to count characters and another pass to construct the output string with a logic that is constant with respect to the number of characters.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?