-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decode String.cpp
44 lines (36 loc) Β· 1.14 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
class Solution {
public:
string decodeString(string s) {
stack<char> st;
for(int i = 0; i < s.size(); i++){
if(s[i] != ']') {
st.push(s[i]);
}
else{
string curr_str = "";
while(st.top() != '['){
curr_str = st.top() + curr_str ;
st.pop();
}
st.pop(); // for '['
string number = "";
// for calculating number
while(!st.empty() && isdigit(st.top())){
number = st.top() + number;
st.pop();
}
int k_time = stoi(number); // convert string to number
while(k_time--){
for(int p = 0; p < curr_str.size() ; p++)
st.push(curr_str[p]);
}
}
}
s = "";
while(!st.empty()){
s = st.top() + s;
st.pop();
}
return s;
}
};