给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9。按升序输出原始的数字。
注意:

  1. 输入只包含小写英文字母。
  2. 输入保证合法并可以转换为原始的数字,这意味着像 “abc” 或 “zerone” 的输入是不允许的。
  3. 输入字符串的长度小于 50,000。

示例 1:

  1. 输入: "owoztneoer"
  2. 输出: "012" (zeroonetwo)

示例 2:

输入: "fviefuro"
输出: "45" (fourfive)

代码

zero z
one o -(0+2+4)
two w
three r - (0 +4)
four u
five f - 4
six x
seven s - 6
eight g
nine i -(5+6+8)

class Solution {
public:
    string originalDigits(string s) {
        unordered_map<char, int> hash_map;
        vector<int> count(10, 0);
        if(s.size() == 0){
            return s;
        }
        for(int i = 0; i<s.size(); i++){
            if(hash_map.find(s[i]) == hash_map.end()){
                hash_map[s[i]] = 1;
            }else{
                hash_map[s[i]] += 1;
            }
        }
        count[0] = hash_map['z'];        
        count[2] = hash_map['w'];       
        count[4] = hash_map['u'];        
        count[6] = hash_map['x'];
        count[5] = hash_map['f'] - count[4];
        count[8] = hash_map['g'];
        count[1] = hash_map['o'] - count[0] - count[2] - count[4];
        count[3] = hash_map['r'] - count[0] - count[4];

        count[7] = hash_map['s'] - count[6];        
        count[9] = hash_map['i'] - count[5] - count[6]- count[8];

        string res;
        for(int i = 0; i<10 ; i++){
            for(int j = 0; j<count[i];j++){
                res += to_string(i); 
            }
        }
        return res;      
    }
};