forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
58 lines (44 loc) · 1.27 KB
/
main.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/// Source : https://leetcode.com/problems/reconstruct-original-digits-from-english/
/// Author : liuyubobobo
/// Time : 2019-02-14
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
/// Ad-Hoc
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
string originalDigits(string s) {
vector<int> freq(26);
for(char c: s)
freq[c - 'a'] ++;
string res = "";
res += check(freq, "zero", 'z', '0');
res += check(freq, "two", 'w', '2');
res += check(freq, "four", 'u', '4');
res += check(freq, "six", 'x', '6');
res += check(freq, "eight", 'g', '8');
res += check(freq, "one", 'o', '1');
res += check(freq, "three", 'r', '3');
res += check(freq, "five", 'f', '5');
res += check(freq, "seven", 'v', '7');
res += check(freq, "nine", 'i', '9');
sort(res.begin(), res.end());
return res;
}
private:
string check(vector<int>& freq, const string& s, char base, char res){
int num = freq[base - 'a'];
for(char c: s){
freq[c - 'a'] -= num;
assert(freq[c - 'a'] >= 0);
}
return string(num, res);
}
};
int main() {
return 0;
}