-
Notifications
You must be signed in to change notification settings - Fork 12
/
Decode String.cpp
46 lines (39 loc) · 1.02 KB
/
Decode 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
class Solution {
public:
int getNumber(string s, int& start, int end){
string ans = "";
while(start < end){
if(!isdigit(s[start])){
break;
}
ans += s[start];
start++;
}
return stoi(ans);
}
string decode(string s, int& start, int end){
string ans = "";
while(start < end){
if(isdigit(s[start])){
int num = getNumber(s, start, end);
start++;
string temp = decode(s, start, end);
for(int i = 0; i < num; i++){
ans += temp;
}
}
else if(s[start] == ']'){
break;
}
else{
ans += s[start];
}
start++;
}
return ans;
}
string decodeString(string s) {
int i = 0, n = s.size();
return decode(s, i, n);
}
};