-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodeString.cpp
More file actions
41 lines (41 loc) · 1 KB
/
Copy pathDecodeString.cpp
File metadata and controls
41 lines (41 loc) · 1 KB
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
36
37
38
39
40
41
class Solution {
public:
string decodeString(string s) {
string out="";
stack<int> s1;
stack<char> s2;
string n="";
for(int i=0;i<s.size();i++){
if(s[i]==']'){
string ts="";
while(s2.top()!='['){
ts+=s2.top();
s2.pop();
}
s2.pop();
int i=s1.top();
s1.pop();
for(i;i>0;i--){
for(int j=ts.size()-1;j>=0;j--){
s2.push(ts[j]);
}
}
}
else{
if(isdigit(s[i])){
n+=s[i];
}
else{
s2.push(s[i]);
if(n!="") s1.push(stoi(n));
n.clear();
}
}
}
while(!s2.empty()){
out=s2.top()+out;
s2.pop();
}
return out;
}
};