Decode String LeetCode solution in C++

1
class Solution {
2
public:
3
string decodeString(string s) {
4
stack<char> st;
5
for (int i = 0; i < s.size(); i++) {
6
if (s[i] != ']') {
7
st.push(s[i]);
8
} else {
9
string curr_str = "";
10

11
while (st.top() != '[') {
12
curr_str = st.top() + curr_str;
13
st.pop();
14
}
15

16
st.pop(); // for '['
17
string number = "";
18

19
// for calculating number
20

21
while (!st.empty() && isdigit(st.top())) {
22
number = st.top() + number;
23
st.pop();
24
}
25
int k_time = stoi(number); // convert string to number
26

27
while (k_time--) {
28
for (int p = 0; p < curr_str.size(); p++) st.push(curr_str[p]);
29
}
30
}
31
}
32

33
s = "";
34
while (!st.empty()) {
35
s = st.top() + s;
36
st.pop();
37
}
38
return s;
39
}
40
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0