[Leetcode] 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 kis 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 or2[4].

Examples:

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

public class Solution {
    private class DigitalValue {
        public int value;
        public int endPosition;
    }
    
    public String decodeString(String s) {
        StringBuilder resultBuilder = decodeStringHelper(s, 1, 0 , s.length());
        return resultBuilder.toString();
    }
    
    // begin inclusive, end exclusive
    private StringBuilder decodeStringHelper(String s, int rep, int begin, int end) {
        StringBuilder result = new StringBuilder();
        for(int i = begin; i < end;) {
            char character = s.charAt(i);
            if(Character.isDigit(character)) {
                DigitalValue digitalValue = getDigitalValue(s, i);
                int charDigitValue = digitalValue.value;
                int endPosition = endBracketPosition(s, digitalValue.endPosition);
                StringBuilder partialResult = decodeStringHelper(s, charDigitValue, digitalValue.endPosition, endPosition);
                result.append(partialResult);
                i = endPosition+1;
            }
            else {
                if(Character.isLetter(character)) {
                    result.append(character);
                }
                i++;
            }
        }
        
        StringBuilder originalResult = new StringBuilder(result);
        for(int i = 0; i < rep - 1; i++) {
            result.append(originalResult);
        }
        return result;
    }
    
    private DigitalValue getDigitalValue(String s, int begin) {
        int value = 0;
        int i = 0;
        for(i = begin; i < s.length(); i++) {
            char character = s.charAt(i);
            if(Character.isDigit(character)) {
                int charDigitValue = Character.digit(character, 10);
                value = 10 * value + charDigitValue;
            }
            else{
                break;
            }
        }
        DigitalValue result = new DigitalValue();
        result.value = value;
        result.endPosition = i;
        return result;
    }
    
    private int endBracketPosition(String s, int left) {
        int bracketCount = 0;
        for(int i = left; i < s.length(); i++) {
            char character = s.charAt(i);
            if(character == '[') {
                bracketCount++;
            }
            else if(character == ']') {
                bracketCount--;
            }
            if(bracketCount == 0) {
                return i;
            }
        }
        return -1;
    }
}


你可能感兴趣的:(leetcode)