Given a non-empty string containing an out-of-order English representation of digits 0-9
, output the digits in ascending order.
Note:
- Input contains only lowercase English letters.
- Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.
- Input length is less than 50,000.
Example 1:
Input: "owoztneoer" Output: "012"
Example 2:
Input: "fviefuro" Output: "45"
class Solution {
public:
string originalDigits(string s) {
vector<int> letters(26,0);
for (auto c: s) {
++letters[c - 'a'];
}
vector<string> words = {"zero", "two", "four", "six", "eight", "one", "three", "five", "seven", "nine"};
vector<int> order = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
vector<char> dis_char = {'z', 'w', 'u', 'x', 'g', 'o', 'r', 'f', 'v', 'i'};
vector<int> counts(10, 0);
for (int i = 0;i < 10; ++i) {
int num = letters[dis_char[i] - 'a'];
counts[order[i]] = num;
for (auto c: words[i]) {
letters[c - 'a'] -= num;
}
}
string result;
for (int i = 0; i < 10; ++i) {
result += string(counts[i], char(i +'0'));
}
return result;
}
};