forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reformat-the-string.cpp
46 lines (43 loc) · 1.14 KB
/
reformat-the-string.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
// Time: O(n)
// Space: O(1)
class Solution {
public:
string reformat(string s) {
unordered_map<char, int> count;
int alpha_cnt = 0;
for (const auto& c : s) {
++count[c];
if (isalpha(c)) {
++alpha_cnt;
}
}
if (abs(int(s.length()) - 2 * alpha_cnt) > 1) {
return "";
}
char a = 'a', a_end = 'z';
char b = '0', b_end = '9';
if (alpha_cnt < s.length() - alpha_cnt) {
swap(a, b);
swap(a_end, b_end);
}
string result;
while (result.length() < s.length()) {
result.push_back(a = next_char(a, a_end, &count));
result.push_back(b = next_char(b, b_end, &count));
}
if (result.back() == '\0') {
result.pop_back();
}
return result;
}
private:
char next_char(char start, char end, unordered_map<char, int> *count) {
for (char c = start; c <= end; ++c) {
if ((*count)[c]) {
--(*count)[c];
return c;
}
}
return '\0';
}
};