-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path394.cpp
More file actions
32 lines (32 loc) · 895 Bytes
/
394.cpp
File metadata and controls
32 lines (32 loc) · 895 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
class Solution {
public:
string recursiveDecode(string& s, int& index) {
string res = "";
int multiply = 0;
while (index < s.size()) {
if (s[index] >= '0' && s[index] <= '9') {
multiply = multiply * 10 + s[index] - '0';
index++;
}
else if (s[index] == '[') {
index++;
string out = recursiveDecode(s, index);
for (int k = 0; k < multiply; ++k) res += out;
multiply = 0;
}
else if (s[index] == ']') {
index++;
return res;
}
else {
res.push_back(s[index]);
index++;
}
}
return res;
}
string decodeString(string s) {
int index = 0;
return recursiveDecode(s, index);
}
};