class Solution {
public String decodeString(String s) {
int multi = 0, i = 0;
StringBuilder res = new StringBuilder();
LinkedList<Integer> multiStack = new LinkedList<>();
LinkedList<String> stringStack = new LinkedList<>();
while (i < s.length()) {
char c = s.charAt(i);
if (c == '[') {
multiStack.offerLast(multi);
stringStack.offerLast(res.toString());
multi = 0;
res = new StringBuilder();
} else if (c == ']') {
StringBuilder tmpBuilder = new StringBuilder();
int tmpMulti = multiStack.removeLast();
for (int j = 0; j < tmpMulti; ++j) {
tmpBuilder.append(res);
}
res = new StringBuilder(stringStack.removeLast() + tmpBuilder.toString());
} else if (c >= '0' && c <= '9') {
multi = 10 * multi + c - '0';
} else {
res.append(c);
}
++i;
}
return res.toString();
}
}