# [LeetCode] Reconstruct Original Digits from English

Reconstruct Original Digits from English

Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.

Note:

1. Input contains only lowercase English letters.
2. 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.
3. Input length is less than 50,000.

Example 1:

Input: "owoztneoer"

Output: "012"

Example 2:

Input: "fviefuro"

Output: "45"

Subscribe to see which companies asked this question.

class Solution {
public:
void update(int* count, string s, int n) {
for (auto c : s) {
count[c - 'a'] -= n;
}
}
string originalDigits(string s) {
vector<string> dict = {"zero", "one", "two", "three", "fore", "five", "six", "seven", "eight", "nine"};
int digitOrder[10] = {0, 8, 6, 2, 3, 7, 5, 4, 1, 9};
char keyOrder[10] = {'z', 'g', 'x', 'w', 'h', 's', 'v', 'f', 'o', 'i'};
int count[26] = {0};
for (auto c : s) {
++count[c - 'a'];
}
int res[10] = {0};
for (int i = 0; i < 10; ++i) {
res[digitOrder[i]] = count[keyOrder[i] - 'a'];
update(count, dict[digitOrder[i]], res[digitOrder[i]]);
}
string str = "";
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < res[i]; ++j) {
str += ('0' + i);
}
}
return str;
}
};

posted @ 2017-04-10 16:28  Eason Liu  阅读(349)  评论(0编辑  收藏  举报