-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode-string.cpp
More file actions
62 lines (57 loc) · 1.47 KB
/
Copy pathdecode-string.cpp
File metadata and controls
62 lines (57 loc) · 1.47 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
54
55
56
57
58
59
60
61
62
//https://leetcode.com/problems/decode-string/
class Solution {
public:
int processDigit(stack<char> &st){
string num="";
while(!st.empty() && st.top()>='0' && st.top()<='9'){
num = string(1,st.top()) + num;
st.pop();
}
stringstream ss;
ss<< num;
int num1;
ss>>num1;
return num1;
}
string preProcessString(stack<char> &st){
string ret="";
while(!st.empty() && st.top()>='a' && st.top()<='z'){
ret = string(1,st.top()) + ret;
st.pop();
}
return ret;
}
string postProcessString(string s1, int m){
string ret = "";
for(int i=0;i<m;i++){
ret = ret + s1;
}
return ret;
}
void postProcess(stack<char> &st, string s1){
for(char c:s1){
st.push(c);
}
}
string decodeString(string s) {
int n = s.length();
stack<char> st;
for(int i=0;i<n;i++){
if(s[i]==']'){
string s1 = preProcessString(st);
st.pop(); // pop '[' from stack
int n1 = processDigit(st);
string s2 = postProcessString(s1, n1);
postProcess(st, s2);
} else {
st.push(s[i]);
}
}
string ans = "";
while(!st.empty()){
ans = string(1,st.top())+ans;
st.pop();
}
return ans;
}
};