1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#include "leetcode.h"
class Solution {
public:
string originalDigits(string s) {
int fMap[26] = {0}, ans[10] = {0};
for (char c : s)
fMap[c - 97]++;
for (int i = 0; i < 10; i++) {
int count = fMap[CHARS[i]];
for (int remain : REM[i])
fMap[remain] -= count;
ans[DIGITS[i]] = count;
}
string ansStr;
for (int i = 0; i < 10; i++) {
char c = (char)(i + 48);
for (int j = ans[i]; j; j--)
ansStr += c;
}
return ansStr;
}
private:
const int DIGITS[10] = {0, 2, 4, 6, 8, 5, 7, 3, 9, 1},
CHARS[10] = {25, 22, 20, 23, 6, 5, 18, 7, 8, 14};
const vector<vector<int>> REM = {{14}, {14}, {5, 14}, {18, 8}, {8, 7},
{8}, {}, {}, {}, {}};
};
int main() {}
|