0%

423. 从英文中重建数字

423. 从英文中重建数字

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
string originalDigits(string s) {
int map[26]{ 0 };
for(auto ch : s)
++map[ch - 'a'];
int cnt[10];
cnt[0] = map['z' - 'a'];
cnt[6] = map['x' - 'a'];
cnt[2] = map['w' - 'a'];
cnt[8] = map['g' - 'a'];
cnt[4] = map['u' - 'a'];
cnt[5] = map['f' - 'a'] - cnt[4];
cnt[3] = map['h' - 'a'] - cnt[8];
cnt[7] = map['v' - 'a'] - cnt[5];
cnt[1] = map['o' - 'a'] - cnt[0] - cnt[2] - cnt[4];
cnt[9] = map['i' - 'a'] - cnt[5] - cnt[6] - cnt[8];
string ret;
for(int i = 0; i < 10; ++i)
for(int j = 0; j < cnt[i]; ++j)
ret += i + '0';
return ret;
}
};