423. Reconstruct Original Digits from English
Description
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.
Solution
reconstruct-original-digits-from-english.py
class Solution:
def originalDigits(self, s: str) -> str:
cnt = collections.Counter(s)
digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
freq = [0] * 10
for x, i in ("z", 0), ("w", 2), ("u", 4), ("x", 6), ("g", 8), ("s", 7), ("f", 5), ("o", 1),("h", 3), ("i", 9):
freq[i] += cnt[x]
cnt -= Counter(digits[i]*cnt[x])
return "".join(str(i)*x for i, x in enumerate(freq))