-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode-string.cpp
35 lines (35 loc) · 916 Bytes
/
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
class Solution {
public:
string decodeString(string s) {
stack<char> st;
for (char c : s) {
if (c != ']') {
st.push(c);
} else {
string curr;
while (st.top() != '[') {
curr = st.top() + curr;
st.pop();
}
st.pop();
string number = "";
while (!st.empty() && isdigit(st.top())) {
number = st.top() + number;
st.pop();
}
int n = stoi(number);
while (n--) {
for (char d : curr) {
st.push(d);
}
}
}
}
s = "";
while (!st.empty()) {
s = st.top() + s;
st.pop();
}
return s;
}
};