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].
Input: s = "3[a]2[bc]"
Output: "aaabcbc"
string decode( string str){
string ret;
for( int i = 0 ; i < str.size() ; i++){
if( str[i] < '0' or str[i] > '9' ) ret.push_back(str[i]);
else {
string inside;
string mult;
while(i < str.size() and ( str[i] >= '0' and str[i] <= '9' )) {
mult.push_back(str[i]);
i++;
}
int m = stoi(mult);
int open = 1, close = 0;
i += 1 ;
while( i < str.size() and open > close ){
if( str[i] == '[' ) open++;
if( str[i] == ']' ) close++;
inside.push_back(str[i]);
i++;
}
inside.pop_back();
i -= 1;
string mid = decode(inside);
while(m--) ret += mid;
}
}
return ret;
}
string decodeString(string & s) {
return decode(s);
}