394. Decode String

Question

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].

Example 1:

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

Thinking:

  • Method: 使用递归
class Solution {
    public static class Result{
        String temp;
        int index;
        public Result(String temp, int index){
            this.temp = temp;
            this.index = index;
        }
    }
    public String decodeString(String s) {
        if(s == null || s.length() == 0) return "";
        return getStringDFS(s.toCharArray(), -1, 0).temp;
    }
    public static Result getStringDFS(char[] arr, int k, int start){
        StringBuilder sb = new StringBuilder();
        int count = 0;
        while(start < arr.length){
            char c = arr[start];
            if(Character.isDigit(c)){
                count = count * 10 + (c - '0');
                start++;
                continue;
            }
            if(c == '['){
                Result res = getStringDFS(arr, count, start + 1);
                count = 0;
                sb.append(res.temp);
                start = res.index + 1;
            }else if(c == ']'){
                String t = sb.toString();
                for(int i = 0; i < k - 1; i++)
                    sb.append(t);
                return new Result(sb.toString(), start);
            }else{
                sb.append(c);
                start++;
            }
        }
        return new Result(sb.toString(), start);
    }
}
  • Method 2: 使用双栈,一个用于储存循环次数,一个用于储存当前的字符串,不断向前append。
class Solution {
    public String decodeString(String s) {
        if(s == null || s.length() == 0) return "";
        Stack<Integer> inStack = new Stack<>();
        Stack<StringBuilder> sStack = new Stack<>();
        StringBuilder sb = new StringBuilder();
        int count = 0;
        for(char c : s.toCharArray()){
            if(Character.isDigit(c)){
                count = count * 10 + (c - '0');
            }else if(c == '['){
                inStack.push(count);
                sStack.push(sb);
                sb = new StringBuilder();
                count = 0;
            }else if(c == ']'){
                StringBuilder temp = sb;
                sb = sStack.pop();
                for(int i = inStack.pop(); i >0; i--)
                    sb.append(temp);
            }else
                sb.append(c);
        }
        return sb.toString();
    }
}

Second Time

  • Method 1: Recursion
    class Solution {
        private char[] arr;
        private int index;
        private String s;
        public String decodeString(String s) {
            this.arr = s.toCharArray();
            this.s = s;
            return decode(arr);
        }
        private String decode(char[] arr){
            String res = getValue();
            if(index < arr.length){
                if(arr[index] == ']'){
                    index++;
                    return res;
                }else{
                    int mul = getCount();
                    index++;    // remove [
                    String sub = decode(arr);
                    for(int i = 0; i < mul; i++){
                        res += sub;
                    }
                }
            }
            if(index < arr.length){
                res += decode(arr);
            }
            return res;
        }
        private int getCount(){
            int count = 0;
            while(index < arr.length && arr[index] >= '0' && arr[index] <= '9'){
                count = count * 10 + arr[index] - '0';
                index++;
            }
            return count == 0 ? 1: count;
        }
        private String getValue(){
            int start = index;
            while(index < arr.length && Character.isLetter(arr[index])){
                index++;
            }
            return s.substring(start, index);
        }
    }