Programming

Leetcode-394. Decode String

Leetcode-394. Decode String

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.

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"

<解題>

  1. 用stack來儲存counts(數字)及result(處理重複字串),最後回傳res(字串)

class Solution {
    public String decodeString(String s) {
        Stack<Integer> counts = new Stack();
        Stack<String> result = new Stack();
        String res = "";
        int index = 0;

        while(index < s.length()){
            if(Character.isDigit(s.charAt(index))){
                int count = 0;
                while(Character.isDigit(s.charAt(index))){
                    count = 10 * count + (s.charAt(index) - '0');
                    index += 1;
                }
                counts.push(count);
            } else if(s.charAt(index) == '['){
                result.push(res);
                res = "";
                index += 1; 
            } else if(s.charAt(index) == ']'){
                StringBuilder temp = new StringBuilder(result.pop());
                int count = counts.pop();
                for(int i = 0; i<count ; i++){
                    temp.append(res);
                }
                res = temp.toString();
                index += 1;
            } else{ //字串
                res += s.charAt(index);
                index += 1;
            }
        } 
        return res;
    }
}

Time: O(n) Space: O(n)


class Solution {
    public String decodeString(String s) {
        int index = 0;
        StringBuilder result = new StringBuilder();

        while (index < s.length()) {
            if (Character.isDigit(s.charAt(index))) {
                int count = 0;
                while (Character.isDigit(s.charAt(index))) {
                    count = 10 * count + (s.charAt(index) - '0');
                    index++;
                }
                index++; // 跳過 '['
                int startIndex = index;
                int endIndex = findClosingBracket(s, startIndex);
                String subString = decodeString(s.substring(startIndex, endIndex)); //包含前不包含後
                for (int i = 0; i < count; i++) {
                    result.append(subString);
                }
                index = endIndex + 1; // 跳過 subString 和 ']'
            } else {
                result.append(s.charAt(index));
                index++;
            }
        }
        return result.toString();
    }

    private int findClosingBracket(String s, int startIndex) {
        int count = 1; // 跳過第一個 '[',所以初始化為 1
        int index = startIndex;

        while (count > 0) {
            char c = s.charAt(index);
            if (c == '[') {
                count++;
            } else if (c == ']') {
                count--;
            }
            index++;
        }
        return index - 1;
    }
}