Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.
Note:
Input contains only lowercase English letters.
Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as “abc” or “zerone” are not permitted.
Input length is less than 50,000.
Example 1:
Input: “owoztneoer”
Output: “012”
Example 2:
Input: “fviefuro”
Output: “45”
Solution:
0 -> only z symbol in this word
2 -> only w symbol in this word
4 -> only u symbol in this word
6 -> only x symbol in this word
8 -> only g symbol in this word
1 -> o symbol only in this word after checked previous few
3 -> t symbol only in this word after checked previous few
5 -> f symbol only in this word after checked previous few
7 -> s symbol only in this word after checked previous few
9 -> n symbol only in this word after checked previous few1
2
3
4
5
6
7
8
9
10
11
12
13
14
15class Solution:
def originalDigits(self, s):
counter = Counter(s)
num2word = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'}
num2target = {0: 'z', 2: 'w', 4: 'u', 6: 'x', 8: 'g',
1: 'o', 3: 't', 5: 'f', 7: 's', 9: 'i'}
multi = [0] * 10
for i in [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]:
word = num2word[i]
targetLet = num2target[i]
if counter[targetLet]:
multi[i] = counter[targetLet]
for w in word:
counter[w] -= multi[i]
return ''.join([str(i)*multi[i] for i in range(10)])