-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDecode the string.cpp
More file actions
48 lines (48 loc) · 1.32 KB
/
Decode the string.cpp
File metadata and controls
48 lines (48 loc) · 1.32 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
42
43
44
45
46
47
48
class Solution{
public:
string decodedString(string s){
stack<string>stkc;
stkc.push("#");
stack<int>stki;
string ans = "";
int cnt = 0;
int cnt1=0;
for(int i = 0 ; i < s.size() ;i++){
if(s[i]-'0' >=0 && s[i]-'0' <=9){
int n = 0;
while(s[i]-'0' >=0 && s[i]-'0' <=9){
n = n*10 + s[i]-'0';
i++;
}
stki.push(n);
i--;
}
else if(s[i]==']'){
string temp="";
while(!stkc.empty() && stkc.top()!="["){
temp = stkc.top() + temp;
stkc.pop();
}
stkc.pop();
ans = temp;
int n = stki.top();
stki.pop();
for(int i = 0 ; i < n-1 ; i++){
ans = ans + temp;
}
stkc.push(ans);
}
else{
string temp = "";
temp += s[i];
stkc.push(temp);
}
}
ans ="";
while(stkc.top()!="#"){
ans = stkc.top() + ans;
stkc.pop();
}
return ans;
}
};