Decode String LeetCode solution in C++
1class Solution {2public:3string decodeString(string s) {4stack<char> st;5for (int i = 0; i < s.size(); i++) {6if (s[i] != ']') {7st.push(s[i]);8} else {9string curr_str = "";1011while (st.top() != '[') {12curr_str = st.top() + curr_str;13st.pop();14}1516st.pop(); // for '['17string number = "";1819// for calculating number2021while (!st.empty() && isdigit(st.top())) {22number = st.top() + number;23st.pop();24}25int k_time = stoi(number); // convert string to number2627while (k_time--) {28for (int p = 0; p < curr_str.size(); p++) st.push(curr_str[p]);29}30}31}3233s = "";34while (!st.empty()) {35s = st.top() + s;36st.pop();37}38return s;39}40};