package leetcode;

import java.util.Stack;

/**
 * https://leetcode.com/problems/decode-string/
 */
public class Problem394 {
    public String decodeString(String s) {
        Stack<String> strings = new Stack<>();
        Stack<Integer> counts = new Stack<>();
        StringBuilder string = new StringBuilder();
        StringBuilder integer = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '[') {
                if (integer.length() > 0) {
                    counts.add(Integer.valueOf(integer.toString()));
                }
                if (string.length() > 0) {
                    strings.add(string.toString());
                }
                string = new StringBuilder();
                integer = new StringBuilder();
            } else if (c == ']') {
                if (integer.length() > 0) {
                    counts.add(Integer.valueOf(integer.toString()));
                }
                if (string.length() > 0) {
                    strings.add(string.toString());
                }
                int count = counts.pop();
                String str = strings.pop();
                StringBuilder newStr = new StringBuilder();
                for (int cnt = 0; cnt < count; cnt++) {
                    newStr.append(str);
                }
                if (!strings.isEmpty()) {
                    String pop = strings.pop();
                    pop += newStr;
                    strings.add(pop);
                } else {
                    strings.add(newStr.toString());
                }
                string = new StringBuilder();
                integer = new StringBuilder();
            } else if (c >= '0' && c <= '9') {
                if (string.length() > 0) {
                    strings.add(string.toString());
                }
                string = new StringBuilder();
                integer.append(c);
            } else {
                if (integer.length() > 0) {
                    counts.add(Integer.valueOf(integer.toString()));
                }
                integer = new StringBuilder();
                string.append(c);
            }
        }
        if (string.length() > 0) {
            strings.add(string.toString());
        }
        StringBuilder result = new StringBuilder();
        for (String str : strings) {
            result.append(str);
        }
        return result.toString();
    }

    public static void main(String[] args) {
        Problem394 prob = new Problem394();
        System.out.println(prob.decodeString("3[a]2[bc]")); // aaabcbc
        System.out.println(prob.decodeString("3[a2[c]]")); // accaccacc
        System.out.println(prob.decodeString("2[b3[a2[c]]]")); // baccaccaccbaccaccacc
        System.out.println(prob.decodeString("2[abc]3[cd]ef")); // abcabccdcdcdef
        System.out.println(prob.decodeString("abc")); // abc
        System.out.println(prob.decodeString("10[ab]")); // abababababababababab
        System.out.println(prob.decodeString("sd2[f2[e]g]i")); // sdfeegfeegi
    }
}

@@ -2157,48 +2157,92 @@
     
+if (
 string
-.append(c);%0A            %7D%0A
+s.size() %3E counts.size()) %7B%0A                    String pop = %22%22;%0A 
         
 %7D%0A  
@@ -2229,34 +2229,35 @@
  = %22%22;%0A         
-%7D%0A
+   
         if (stri
@@ -2256,72 +2256,72 @@
 if (
+!
 string
-.length() %3E 0) %7B%0A            strings.add(string.toString()
+s.isEmpty()) %7B%0A                        pop = strings.pop(
 );%0A 
@@ -2327,26 +2327,24 @@
         
-%7D%0A
         
 StringBu
@@ -2339,120 +2339,80 @@
     
-StringBuilder result = new StringBuilder();%0A        for (String str : strings) %7B%0A            result.append(str);
+    %7D%0A                    strings.add(pop + c);%0A                %7D else %7B
 %0A   
@@ -2416,26 +2416,24 @@
         
-%7D%0A
         
 return r
@@ -2428,661 +2428,325 @@
     
-return result.toString(
+    string.append(c
 );%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem394 prob = new Problem394();%0A
+        %7D%0A         
    
+%7D%0A
      
-System.out.println(prob.decodeString(%223%5Ba%5D2%5Bbc%5D%22)); // aaabcbc%0A        System.out.println(prob.decodeString(%223%5Ba2%5Bc%5D%5D%22)); // accaccacc%0A        System.out.println(prob.decodeString(%222%5Bb3%5Ba2%5Bc%5D%5D%5D%22)); // baccaccaccbaccaccacc%0A        System.out.println(prob.decodeString(%222%5Babc%5D3%5Bcd%5Def%22)); // abcabccdcdcdef%0A        System.out.println(prob.decodeString(%22abc%22)); // abc%0A        System.out.println(prob.decodeString(%2210%5Bab%5D%22)); // abababababababababab%0A        System.out.println(prob.decodeString(%22sd2%5Bf2%5Be%5Dg%5Di%22)); // sdfeegfeegi
+   %7D%0A        if (string.length() %3E 0) %7B%0A            strings.add(string.toString());%0A        %7D%0A        StringBuilder result = new StringBuilder();%0A        for (String str : strings) %7B%0A            result.append(str);%0A        %7D%0A        return result.toString();
 %0A   

Solve problem 394