找规律
C++ 代码
class Solution {
public:
string originalDigits(string s) {
// zero: z
// one:2-o
// two:w
// three:2-r
// four:u
// five: 3-v
// six: x
// seven: 2-s
// eight: g
// nine: 4-i
unordered_map<char, int> cnt;
for(char c : s)cnt[c]++;
vector<string> nums_str = {"zero", "two", "four", "six", "eight", "one", "three", "seven", "five", "nine"};
vector<int> nums = {0, 2, 4, 6, 8, 1, 3, 7, 5, 9};
string res;
for(int i=0; i<nums.size(); ++i) {
int num = nums[i];
string str = nums_str[i];
while(1) {
bool exit = true;
// 判断该单词是否存在
for(char c : str) {
// 这里判断该单词是否存在即可,不用枚举特殊字符
if(!cnt[c]){
exit = false;
break;
}
}
if(!exit)break;
res += to_string(num);
for(char c : str)cnt[c]--;
}
}
sort(res.begin(), res.end());
return res;
}
};