<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 {
    std::string result;
    stack<string> St;
    int N;
    string expand(string s, int num){
        string result;
        for (int i=0; i<num; ++i){
            result=result+s;
        }
        return result;
    }

    int find_end_of_string(string s){
        int count=1;
        int i=0;
        while (count){
            if (s[i]==']') count--;
            if (s[i]=='[') count++;
            i++;
        }
        return i-2;
    }
    bool need_more_expanding(string s){
        return s.find_first_of("[")==std::string::npos;
    }

    int get_number(string s, int &expand_num){
        //std::cout<<"getting number of "<<s<<std::endl;
        int i=0;
        int n=s.length();
        int length=0;
        expand_num=0;
        while (isdigit(s[i])){
            length++;
            expand_num=expand_num*10+s[i]-'0';
            //std::cout<<"expand_num="<<expand_num<<std::endl;
            i++;
        }
        return length;
    }
    string parseAndExpand(string s){
        std::cout<<"Prase and Expand "<<s<<std::endl;
        string result;
        int n=s.length();
        int i=0;
        while(i<n){
            if (isdigit(s[i])){
                int expand_num;
                int length=get_number(s.substr(i,n-i), expand_num);
                //std::cout<<"Length = "<<length<<" expand_num="<<expand_num<<std::endl;
                
                if (s[i+length]=='['){
                    int start = i+length+1;
                    int end = find_end_of_string(s.substr(start,n-start+1));
                    end = start + end;
                    string str_to_expand = s.substr(start,end-start+1);
                    //std::cout<<"Expanding "<<str_to_expand<<std::endl;
                    auto str = expand(str_to_expand,expand_num);
                    result=result+str;
                    i=end+2;
                }
            }
            else{
                result=result+s[i];
                i++;
            }
           
        }
        bool done = need_more_expanding(result);
        if (!done) {
            return parseAndExpand(result);
        }
        return result;
    }

public:
    string decodeString(string s) {
        N=s.length();
        return parseAndExpand(s);
    }
};
9500