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"].
- sis guaranteed to be valid.
Solution
Python3
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))