<p>Given an encoded string, return its decoded string.</p>

<p>The encoding rule is: <code>k[encoded_string]</code>, where the <code>encoded_string</code> inside the square brackets is being repeated exactly <code>k</code> times. Note that <code>k</code> is guaranteed to be a positive integer.</p>

<p>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, <code>k</code>. For example, there will not be input like <code>3a</code> or <code>2[4]</code>.</p>

<p>The test cases are generated so that the length of the output will never exceed <code>10<sup>5</sup></code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;3[a]2[bc]&quot;
<strong>Output:</strong> &quot;aaabcbc&quot;
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;3[a2[c]]&quot;
<strong>Output:</strong> &quot;accaccacc&quot;
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;2[abc]3[cd]ef&quot;
<strong>Output:</strong> &quot;abcabccdcdcdef&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 30</code></li>
	<li><code>s</code> consists of lowercase English letters, digits, and square brackets <code>&#39;[]&#39;</code>.</li>
	<li><code>s</code> is guaranteed to be <strong>a valid</strong> input.</li>
	<li>All the integers in <code>s</code> are in the range <code>[1, 300]</code>.</li>
</ul>

class Solution {
public:
    string decodeString(string s) {
        stack<int> st;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] != ']') {
                st.push(s[i]);
                continue;                
            }
            // pop till '[' then digits
            string temp;
            while (st.top() != '[') {
                auto nextt = st.top(); st.pop();
                temp.push_back(nextt);
            }
            reverse(temp.begin(), temp.end());
            st.pop();

            int cnt = 0;
            int multi = 1;
            while (!st.empty() && isdigit(st.top())) {
                cnt = (st.top() - '0') * multi + cnt;
                st.pop(); 
                multi *= 10;
            }
            // do decode, then push back to stack
            string decode;
            for (int dup = 0; dup < cnt; dup++) {
                decode += temp;
            }
            // cout << "decode=" << decode << ", temp=" << temp
            //      << ", cnt=" << cnt << endl;
            for (auto c : decode) st.push(c);
        }
        string res;
        while (!st.empty()) {
            res.push_back(st.top());
            st.pop();
        }
        reverse(res.begin(), res.end());
        return res;
    }
    // string decodeString(string s) {
    //     int i = 0;
    //     string res;
    //     stack<int> repeats;
    //     stack<string> strs;
    //     while (i < s.length()) {
    //         if (isdigit(s[i])) {
    //             int v = 0;
    //             while (isdigit(s[i])) {
    //                 v = v * 10 + s[i] - '0';
    //                 i++;
    //             }
    //             repeats.push(v);
    //         } else if (s[i] == '[') {
    //             strs.push("[");
    //             i++;
    //         } else if (s[i] == ']') {
    //             string sub;
    //             while (strs.top() != "[") {
    //                 sub = strs.top() + sub; 
    //                 strs.pop();
    //             }                
    //             int r = repeats.top(); repeats.pop();
    //             string str;
    //             while (r-- > 0) {
    //                 str += sub;
    //             }
    //             // prev '['
    //             strs.pop();
    //             if (repeats.empty()) {
    //                 res.append(str);
    //             } else {
    //                 strs.push(str);
    //             }
    //             i++;
    //         } else {
    //             string str;
    //             while (i < s.size() && isalpha(s[i])) {
    //                 str.append(1, s[i]);
    //                 i++;
    //             }
    //             if (repeats.empty()) {
    //                 res.append(str);
    //             } else {
    //                 strs.push(str);
    //             }
    //         }            
    //     }
    //     return res;     
    // }
};
8900