-
Notifications
You must be signed in to change notification settings - Fork 1
/
11_October.cpp
35 lines (35 loc) · 945 Bytes
/
11_October.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 decodedString(string s){
int n=s.size(), cnt=0;
stack<string> st;
string ans;
for(int i=0; i<n; i++) {
if(s[i]!=']') {
string temp;
temp+=s[i];
st.push(temp); continue;
}
string curr;
while(!st.empty()&&st.top()!="[") {
curr+=st.top();
st.pop();
}
st.pop();
string count;
while(!st.empty()&&st.top()>="0"&&st.top()<="9") {count+=st.top(); st.pop();}
reverse(count.begin(), count.end());
cnt=stoi(count);
string str;
while(cnt--) {
str+=curr;
}
st.push(str);
}
while(!st.empty()) {
ans+=st.top(); st.pop();
}
reverse(ans.begin(), ans.end());
return ans;
}
};