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.

&nbsp;
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"


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 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:
    def decodeString(self, s):
        it, num, stack = 0, 0, [""]
        while it < len(s):
            if s[it].isdigit():
                num = num * 10 + int(s[it])
            elif s[it] == "[":
                stack.append(num)
                num = 0
                stack.append("")
            elif s[it] == "]":
                str1 = stack.pop()
                rep = stack.pop()
                str2 = stack.pop()
                stack.append(str2 + str1 * rep)
            else:
                stack[-1] += s[it]              
            it += 1           
        return "".join(stack)
class Solution {
    public String decodeString(String s) {

        int bb = s.indexOf('['); // location of beginning bracket
        int nbb = s.indexOf('[', bb + 1); // location of next beginning bracket
        int eb = s.indexOf(']'); // location of ending bracket

        int n = 0; // number of times to repeat
        int nl = 1; // number of digits for n
        char nd; // next digit

        String insert = ""; // repeated string
        String end = ""; // remainder of string after repeated portion

        while (bb != -1) { // while the string contains a beginning bracket

            while (nbb < eb && nbb > bb) { // while the next beginning bracket is before the ending bracket
                bb = nbb; // update location of beginning bracket
                nbb = s.indexOf('[', bb + 1); // update location of next beginning bracket
            }

            nl = 1; // reset length of n
            while (bb - nl >= 0) { // while there are characters in front of the beginning bracket
                nd = s.charAt(bb - nl); // next digit
                if (nd <= '9' && nd >= '0') { // if next digit is an integer
                    n += (int)(nd - '0') * Math.pow(10, nl - 1); // update value of n
                    nl++; // increment length of n
                }
                else break; // not an integer
            }

            insert = s.substring(bb + 1, eb); // set repeated string
            end = s.substring(eb + 1); // set remainder of string
            s = s.substring(0, bb - nl + 1); // remove everything after the digits

            while (n > 0) {
                s += insert; // add repeated string n times
                n--;
            }
            s += end; // add remainder of string

            bb = s.indexOf('['); // new location of beginning bracket
            nbb = s.indexOf('[', bb + 1); // new location of next beginning bracket
            eb = s.indexOf(']'); // new location of ending bracket
        }
        return s;
    }
}
class Solution {
public:

    string decodeString(string s) {
        stack<char> st;
        for(int i = 0; i < s.size(); i++){
            if(s[i] != ']') {
                st.push(s[i]);
            }
            else{
                string curr_str = "";
                
                while(st.top() != '['){
                    curr_str = st.top() + curr_str ;
                    st.pop();
                }
                
                st.pop();   // for '['
                string number = "";
                
                // for calculating number
                
                while(!st.empty() && isdigit(st.top())){
                    number = st.top() + number;
                    st.pop();
                }
                int k_time = stoi(number);    // convert string to number
                
                while(k_time--){
                    for(int p = 0; p < curr_str.size() ; p++)
                        st.push(curr_str[p]);
                }
            }
        }
        
        s = "";
        while(!st.empty()){
            s = st.top() + s;
            st.pop();
        }
        return s;
        
    }
};
var decodeString = function(s) {
    const stack = [];

    for (let char of s) {
         if (char === "]") {
            let curr = stack.pop();
            let str = '';
            while (curr !== '[') {
                str = curr+ str;
                curr = stack.pop();
            }
            let num = "";
            curr = stack.pop();
            while (!isNaN(curr)) {
                num = curr + num;
                curr = stack.pop();
            }
            stack.push(curr);
            for (let i=0; i<Number(num);i++) {
                stack.push(str);
            }
        }
        else stack.push(char);
    }

    return stack.join('');
};
Decode String