-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC0423.cpp
More file actions
executable file
·39 lines (34 loc) · 892 Bytes
/
LC0423.cpp
File metadata and controls
executable file
·39 lines (34 loc) · 892 Bytes
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
/*
Problem Statement: https://leetcode.com/problems/reconstruct-original-digits-from-english/
Time: O(n)
Space: O(n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
string originalDigits(string s) {
string digits;
vector<int> cnt(10), freq(26);
vector<string> mp = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
};
vector<pair<int, char>> order = {
{0, 'z'}, {2, 'w'}, {4, 'u'}, {6, 'x'}, {8, 'g'},
{1, 'o'}, {3, 'r'}, {5, 'f'}, {7, 's'}, {9, 'i'}
};
// preprocess
for (char& c: s)
freq[c - 'a']++;
// compute the count of each digit
for (auto& [d, k]: order) {
cnt[d] = freq[k - 'a'];
for (char& c: mp[d])
freq[c - 'a'] -= cnt[d];
}
// create the original digits
for (int i = 0; i < cnt.size(); i++)
digits += string(cnt[i], i + '0');
return digits;
}
};