-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecode_Strings.cpp
More file actions
33 lines (31 loc) · 843 Bytes
/
Decode_Strings.cpp
File metadata and controls
33 lines (31 loc) · 843 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:
string decodeString(string s) {
stack<int> st;
string ans = "";
int n = 0;
for (auto ch : s) {
if (isdigit(ch)) {
n = (n * 10) + (int(ch) - int('0'));
}
else if (ch == '[') {
st.push(n);
n = 0;
st.push(ans.length());
}
else if (ch == ']') {
int left = st.top(); st.pop();
int rep = st.top() - 1; st.pop();
while (rep > 0) {
int len = ans.length();
while (left < len) { ans += ans[left]; left++; }
rep--;
}
}
else {
ans += ch;
}
}
return ans;
}
};