package com.fishercoder.solutions;

import java.util.Stack;

public class _394 {

    public static class Solution1 {
        public String decodeString(String s) {
            Stack<Integer> count = new Stack<>();
            Stack<String> str = new Stack<>();

            int idx = 0;
            str.push("");

            while (idx < s.length()) {
                if (s.charAt(idx) >= '0' && s.charAt(idx) <= '9') {
                    int start = idx;
                    while (s.charAt(idx + 1) >= '0' && s.charAt(idx + 1) <= '9') {
                        idx++;
                    }

                    count.push(Integer.parseInt(s.substring(start, idx + 1)));
                } else if (s.charAt(idx) == '[') {
                    str.push("");
                } else if (s.charAt(idx) == ']') {
                    String st = str.pop();
                    StringBuilder sb = new StringBuilder();
                    int n = count.pop();

                    for (int j = 0; j < n; j++) {
                        sb.append(st);
                    }

                    str.push(str.pop() + sb.toString());
                } else {
                    str.push(str.pop() + s.charAt(idx));
                }

                idx++;
            }

            return str.pop();
        }
    }
}

@@ -54,16 +54,743 @@
 Stack;%0A%0A
+/**394. Decode String%0A *%0A * Given an encoded string, return it's decoded string.%0A * The encoding rule is: k%5Bencoded_string%5D, where the encoded_string inside the square brackets is being repeated exactly k times.%0A * Note that k is guaranteed to be a positive integer.%0A * You may assume that the input string is always valid;%0A * No extra white spaces, square brackets are well-formed, etc.%0A * Furthermore, you may assume that the original data does not contain any digits and%0A * that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2%5B4%5D.%0A *%0A * Examples:%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 * */%0A%0A
 public c

refactor 394