-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecode String.cpp
57 lines (56 loc) · 1.45 KB
/
Decode String.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
public:
string decodeString(string s) {
stack<string> st;
stack<string> st2;
int i = 0;
string temp = "";
while(i < s.length()){
if(s[i] == ']'){
temp = "";
while(!isdigit(st.top()[0])){
st2.push(st.top());
st.pop();
}
while(!st2.empty()){
temp += st2.top();
st2.pop();
}
int k = stoi(st.top());
st.pop();
string ans = "";
while(k--){
ans += temp;
}
st.push(ans);
i++;
}
else if(isdigit(s[i])){
temp = "";
while(isdigit(s[i])){
temp.push_back(s[i++]);
}
st.push(temp);
i++;
}
else{
temp = "";
while(i < s.length() && s[i] >= 'a' && s[i] <= 'z'){
temp.push_back(s[i++]);
}
st.push(temp);
}
}
string ans = "";
while(!st.empty()){
st2.push(st.top());
st.pop();
}
while(!st2.empty()){
cout<<st2.top()<<endl;
ans += st2.top();
st2.pop();
}
return ans;
}
};