-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path394-Decode-String.cpp
More file actions
34 lines (34 loc) · 954 Bytes
/
394-Decode-String.cpp
File metadata and controls
34 lines (34 loc) · 954 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
class Solution {
public:
string decodeString(string s) {
stack<string> st;
for(int i = 0 ; i < s.size() ; i++){
if(s[i]==']'){
string temp;
while(st.top() != \[\){
temp = temp + st.top();
st.pop();
}
st.pop();
string numStr =\\;
while (!st.empty() && isdigit(st.top()[0])) {
numStr = st.top() + numStr;
st.pop();
}
int num = stoi(numStr);
for(int i = 0; i < num ; i++){
st.push(temp);
}
}
else{
st.push(string(1, s[i]));
}
}
string ans = \\;
while(!st.empty()){
ans = ans + st.top(); st.pop();
}
reverse(ans.begin() , ans.end());
return ans;
}
};