@@ -0,0 +1,1917 @@
+package dtprogrammer.github.io.p394;%0A%0A/**%0A * 394. Decode String%0A * Given an encoded string, return it's decoded string.%0A * %3Cp%3E%0A * The encoding rule is: k%5Bencoded_string%5D, where the encoded_string inside the square brackets is being repeated%0A * exactly k times. Note that k is guaranteed to be a positive integer.%0A * %3Cp%3E%0A * You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.%0A * %3Cp%3E%0A * Furthermore, you may assume that the original data does not contain any digits and that digits are only for those%0A * repeat numbers, k. For example, there won't be input like 3a or 2%5B4%5D.%0A * %3Cp%3E%0A * Examples:%0A * %3Cp%3E%0A * s = %223%5Ba%5D2%5Bbc%5D%22, return %22aaabcbc%22.%0A * s = %223%5Ba2%5Bc%5D%5D%22, return %22accaccacc%22.%0A * s = %222%5Babc%5D3%5Bcd%5Def%22, return %22abcabccdcdcdef%22.%0A * %3Cp%3E%0A * Time complexity: O(n)%0A */%0Apublic class Solution %7B%0A    public String decodeString(String s) %7B%0A        int%5B%5D index = new int%5B1%5D;%0A        return decodeString(s, index);%0A    %7D%0A%0A    public String decodeString(String s, int%5B%5D index) %7B%0A        StringBuilder builder = new StringBuilder();%0A        while (index%5B0%5D %3C s.length()) %7B%0A            if (Character.isDigit(s.charAt(index%5B0%5D))) %7B%0A                int repetitions = 0;%0A                while (Character.isDigit(s.charAt(index%5B0%5D))) %7B%0A                    repetitions = repetitions * 10 + s.charAt(index%5B0%5D++) - '0';%0A                %7D%0A                index%5B0%5D++; // skip %5B%0A                String inner = decodeString(s, index);%0A                while (repetitions %3E 0) %7B%0A                    builder.append(inner);%0A                    repetitions--;%0A                %7D%0A            %7D else if (Character.isLetter(s.charAt(index%5B0%5D))) %7B%0A                builder.append(s.charAt(index%5B0%5D++));%0A            %7D else if (s.charAt(index%5B0%5D) == '%5D') %7B%0A                index%5B0%5D++;%0A                return builder.toString();%0A            %7D%0A        %7D%0A        return builder.toString();%0A    %7D%0A%7D%0A

394. Decode String