forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0394.cpp
More file actions
33 lines (33 loc) · 727 Bytes
/
Copy path0394.cpp
File metadata and controls
33 lines (33 loc) · 727 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
class Solution {
public:
std::string decode(std::string &s, int &n, int &i, int times = 1) {
std::string decoded{}, res{};
while (i < n) {
char ch = s[i];
if (int(ch - '0') >= 0 and int(ch - '0') <= 9) {
std::string val{};
while (s[i] != '[') {
val += s[i];
i++;
}
decoded += decode(s, n, ++i, std::stoi(val));
} else {
if (s[i] != ']') {
decoded += s[i++];
} else {
i++;
break;
}
}
}
while (times--) {
res += decoded;
}
return res;
}
string decodeString(string s) {
int i{0}, n{int(s.length())};
std::string ans{decode(s, n, i)};
return ans;
}
};