-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathReconstructOriginalDigitsFromEnglish.cpp
82 lines (77 loc) · 1.81 KB
/
ReconstructOriginalDigitsFromEnglish.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Given a non-empty string containing
an out-of-order English representa-
tion of digits 0-9,output the digits
in ascending order.
Note:
Input contains only lowercase English
letters.
Input is guaranteed to be valid and
can be transformed to its original
digits.That means invalid inputs such
as "abc" or "zeroe" are not permitted.
*/
#include <bits/stdc++.h>
using namespace std;
string EnglishToDigit(string English)
{
long long int L, i, Z, Max;
string Temp, Digit;
vector<pair<string, int> > M;
L = English.length();
int count[26] = { 0 };
for (i = 0; i < L; i++) {
count[English[i] - 'a']++;
}
M.push_back({ "six", 6 });
M.push_back({ "zero", 0 });
M.push_back({ "seven", 7 });
M.push_back({ "eight", 8 });
M.push_back({ "five", 5 });
M.push_back({ "four", 4 });
M.push_back({ "three", 3 });
M.push_back({ "two", 2 });
M.push_back({ "one", 1 });
M.push_back({ "nine", 9 });
for (auto it = M.begin(); it != M.end(); it++) {
Max = INT_MAX;
Temp = it->first;
Z = Temp.length();
for (i = 0; i < Z; i++) {
if (count[Temp[i] - 'a'] < Max) {
Max = count[Temp[i] - 'a'];
}
}
for (i = 0; i < Max; i++) {
Digit += ('0' + it->second);
}
for (i = 0; i < Z; i++) {
count[Temp[i] - 'a'] -= Max;
}
}
sort(Digit.begin(), Digit.end());
return Digit;
}
int main()
{
string English, Digit;
cout << "Enter the String" << endl;
cin >> English;
Digit = EnglishToDigit(English);
cout << "Coverted String" << endl;
cout << Digit << endl;
return 0;
}
/*
Time Complexity:O(N)
Space Complexity:O(1)
Where N is the length
of the string.
Sample I/O
Input:
Enter the String
owoztneoer
Output:
Coverted String
012
*/