-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode_string.cpp
More file actions
43 lines (41 loc) · 1.21 KB
/
Copy pathdecode_string.cpp
File metadata and controls
43 lines (41 loc) · 1.21 KB
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
//submissions link: https://leetcode.com/problems/decode-string/submissions/1968450169/
class Solution {
public:
string decodeString(string s) {
int n = s.size();
int cnt = 0, brc = 0;
string result, rpt;
for(int i=0; i<n; i++){
if(s[i] >= '0' and s[i] <= '9'){
if(brc){
rpt.push_back(s[i]);
continue;
}
cnt *= 10;
cnt += (s[i] - '0');
}
else if(s[i] == '[' or s[i] == ']'){
if(s[i] == '['){
brc++;
if(brc > 1) rpt.push_back(s[i]);
}
else if(brc == 1){
string tmp = decodeString(rpt);
for(int j=0; j<cnt; j++) result += tmp;
cnt = 0;
brc = 0;
rpt.clear();
}
else{
brc--;
rpt.push_back(s[i]);
}
}
else{
if(cnt == 0) result.push_back(s[i]);
else rpt.push_back(s[i]);
}
}
return result;
}
};