forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1417.cpp
27 lines (25 loc) · 754 Bytes
/
1417.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
class Solution {
public:
string reformat(string s) {
stringstream schars, snums;
for (char c : s) {
if (c >= '0' && c <= '9') snums << c;
else schars << c;
}
string chars = schars.str(), nums = snums.str();
if (abs(int(chars.size() - nums.size())) > 1) return "";
stringstream res;
bool flag = nums.size() >= chars.size() ? true : false;
for (int i = 0; i < s.size(); i++) {
if (flag) {
res << nums.back();
nums.pop_back();
} else {
res << chars.back();
chars.pop_back();
}
flag = !flag;
}
return res.str();
}
};