First, calculate how many times each letter appeared in the string
s
, and then for each number in the
class Solution {
public:
string originalDigits(string s) {
vector<string> words = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
vector<int> nums = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
vector<int> distinct = {'z', 'o', 'w', 'h', 'u', 'f', 'x', 's', 'g', 'i'};
vector<int> lcounts(26, 0);
vector<int> dcounts(10, 0);
string res = "";
for(auto c : s) lcounts[c - 'a']++;
for(auto num : nums){
dcounts[num] = lcounts[distinct[num] - 'a'];
for(char c : words[num])
lcounts[c - 'a'] -= dcounts[num];
}
for(int i = 0; i < 10; i++)
for(int j = 0; j < dcounts[i]; j++)
res += to_string(i);
return res;
}
};