#include using namespace std; /* Problem 394. Decode String Medium Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there will not be input like 3a or 2[4]. The test cases are generated so that the length of the output will never exceed 105. Example 1: Input: s = "3[a]2[bc]" Output: "aaabcbc" Example 2: Input: s = "3[a2[c]]" Output: "accaccacc" Example 3: Input: s = "2[abc]3[cd]ef" Output: "abcabccdcdcdef" Constraints: 1 <= s.length <= 30 s consists of lowercase English letters, digits, and square brackets '[]'. s is guaranteed to be a valid input. All the integers in s are in the range [1, 300]. */ class Solution { public: deque sStack; // stack is a wrapper around deque stack kStack; //////////////////////////////////////// // parseK int parseK(const string& s, size_t index) { deque que; while (s.at(index) != '[') que.push_back(s.at(index++) - '0'); int k = 0, i = 0; while (!que.empty()) { int digit = que.back(); k += digit * pow(10, i++); que.pop_back(); } kStack.push(k); return index; } int parseKNoQue(const string& s, size_t index) { int k = 0; size_t i = s.find_first_of('[', index) - index - 1; while(s.at(index) != '[') { int digit = s.at(index++) - '0'; k += digit * pow(10, i--); } kStack.push(k); return index; } string decodeString(string s) { size_t index = 0; string result; while (index < s.size()) { if (isdigit(s.at(index))) { int k = 0; size_t exp = s.find_first_of('[', index) - index - 1; while(s.at(index) != '[') { int digit = s.at(index++) - '0'; k += digit * pow(10, exp--); } kStack.push(k); } else if (s.at(index) == '[') { cout << "push result: " << result << endl; sStack.push_front(result); result.clear(); index++; } else if (s.at(index) == ']') { cout << "index: " << index << " result: " << result << " stack: "; for (auto i: sStack) {cout << i << " ";} cout << endl; int k = kStack.top(); string next = sStack.front(); for (int i = 0; i < k; i++) next += result; result = next; kStack.pop(); sStack.pop_front(); index++; } else if (s.at(index) >= 'a' && s.at(index) <= 'z') result += s.at(index++); } return result; } }; int main() { vector> testCases = { {"3[a]2[bc]","aaabcbc"}, {"3[a2[c]]","accaccacc"}, // acc repeats {"xxx3[a2[c]]","accaccacc"}, // xxx then acc repeats {"2[abc]3[cd]ef","abcabccdcdcdef"}, {"12[a]","aaaaaaaaaaaa"}, {"1[x2[y3[z]]]","xyzzzyzzz"}, // xyzzz repeats {"3[a2[z]]","azzazzazz"}, // azz repeats {"3[a2[z1[f]]]","azfzfazfzfazfzf"}, // azfzf repeats }; for (auto& tc: testCases) { Solution s; string result = s.decodeString(get<0>(tc)); cout << " input: " << get<0>(tc) << endl; cout << " output: " << result << endl; cout << "expected: " << get<1>(tc) << endl; cout << "-------------------\n"; } string s = "abcde4["; int index = 5; Solution ss; cout << "parseK: " << ss.parseK(s, index) << " kStack.top: " << ss.kStack.top() << endl; Solution sss; cout << "parseKNoQue: " << sss.parseKNoQue(s, index) << " kStack.top: " << sss.kStack.top() << endl; }