-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodeString.cpp
More file actions
35 lines (30 loc) · 835 Bytes
/
DecodeString.cpp
File metadata and controls
35 lines (30 loc) · 835 Bytes
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
class Solution {
public:
string decodeString(string s, int& i) {
string result;
while(i<s.length() && s[i]!=']'){
if(!isdigit(s[i])){
result+=s[i++];
}
else{
int n =0;
while (i < s.length() && isdigit(s[i])){
n = n * 10 + s[i++] - '0';
}
string t;
i++;
t = decodeString(s, i);
i++;
while(n-- >0){
result += t;
}
}
}
return result;
}
string decodeString(string s) {
if(!s.length()) return "";
int i =0;
return decodeString(s,i);
}
};