394. Decode String

Given an encoded string, return it's 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; 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 won't be input like 3a or 2[4].

Examples:

s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

Solution

(1) Java

class Solution {
    public String decodeString(String s) {
        if (s == null || s.length() == 0) {
            return "";
        }
        Deque<String> stack = new ArrayDeque<>();
        char[] sa = s.toCharArray();
        String number = "";
        for (char c : sa) {
            if (Character.isDigit(c)) {
                number += c;
            } else if (c == '[') {
                int num = Integer.parseInt(number);
                stack.push(Integer.toString(num));
                stack.push("]");
                number = "";
            } else if (c == ']') {
                StringBuilder sb = new StringBuilder();
                String strstr = stack.pop();
                while (!strstr.equals("]")) {
                    sb.append(strstr);
                    strstr = stack.pop();
                }
                strstr = sb.toString();
                int num = Integer.parseInt(stack.pop());
                sb = new StringBuilder();
                for (int i = 0; i < num; i++) {
                    sb.append(strstr);
                }
                stack.push(sb.toString());
            } else {
                stack.push(Character.toString(c));
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.append(stack.pop());
        }
        return sb.reverse().toString();
    }
}

(2) Python

class Solution:
    def decodeString(self, s):
        """
        :type s: str
        :rtype: str
        """
        if not s:
            return ""
        stack = collections.deque() 
        i = 0
        ss = ""
        while i < len(s) and (s[i] < "0" or s[i] > "9"):
            ss += s[i]
            i += 1
        if i != 0:
            stack.append(ss)
            s = s[i:]
            i = 0

        while i < len(s):
            c = s[i]
            num = ""
            while i < len(s) and c >= "0" and c <= "9":               
                num += c 
                i += 1
                c = s[i]  
            #print("!!!!!!!!s={0},c={1},num={2},stack={3}".format(s,c,num,stack))
            if len(num) > 0:
                stack.append(num)
            #print("@@@ i={0},c={1},num={2},stack={3}".format(i,c,num,stack))
            if c == "[":
                stack.append("[")
                i += 1
            elif c == "]":               
                ss = ""
                cc = stack.pop()
                while cc != "[":
                    ss = cc+ss
                    #print("### i={0},cc={1},stack={2}".format(i,cc,stack))
                    cc = stack.pop()
                #print("###11 i={0},cc={1},stack={2}".format(i,cc,stack))
                cc = stack.pop()
                #print("###22 i={0},cc={1},stack={2}".format(i,cc,stack))
                number = int(cc)
                sss = ss
                for j in range(number-1):
                    sss += ss
                stack.append(sss)
                i += 1
                if i < len(s) and s[i] < "0" and s[i] > "9":
                    w = ""
                    t = s[i]
                    while i < len(s) and t < "0" and t > "9":
                        w += t
                        i += 1
                        t = s[i]
                    if len(w) > 0:
                        stack.append(w)
            else:
                stack.append(c)
                i += 1
        rst = ""
        while len(stack) > 0:
            rst = stack.pop() + rst
        return rst

(3) Scala



results matching ""

    No results matching ""