-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecodeString.cpp
More file actions
53 lines (53 loc) · 1.45 KB
/
decodeString.cpp
File metadata and controls
53 lines (53 loc) · 1.45 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
49
50
51
52
53
#include <stack>
#include <cstring>
class Solution {
public:
string decodeString(string s) {
stack<char> ans;
string temp; //to store the strings to be reversed or multiplied
char ch;
char k;
for (int i=0; i<s.length(); i++) {
ch = s[i];
ans.push(ch);
if(ch == ']') {
ans.pop();
temp = "";
while(true) {
k = ans.top();
ans.pop();
if(k!='[')
temp = temp + k;
else
break;
}
k = ans.top();
int m = 0;
int pos = 1;
while(!ans.empty() && isdigit(ans.top())){
m = m + (ans.top()-'0')*pos;
ans.pop();
pos *= 10;
}
string tmp = "";
while(m){
tmp += temp;
m--;
}
for(int r=tmp.length()-1; r>=0; r--) {
ans.push(tmp[r]);
}
}
}
temp = "";
while(!ans.empty()){
temp = temp + ans.top();
ans.pop();
}
string final_ans = "";
for(int r=temp.length()-1; r>=0; r--) {
final_ans += temp[r];
}
return final_ans;
}
};