public class Solution {
public String decodeString(String s) {
Stack<Integer> intStack = new Stack<>();
Stack<StringBuilder> strStack = new Stack<>();
int count = 0;
StringBuilder cur = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) {
count = count * 10 + (c - '0');
}else if (c == '[') {
intStack.push(count);
strStack.push(cur);
count = 0;
cur = new StringBuilder();
}else if (c == ']') {
count = intStack.pop();
StringBuilder tmp = cur;
cur = strStack.pop();
for (int i = 0; i < count; i++) {
cur.append(tmp);
}
count = 0;
} else {
cur.append(c);
}
}
return cur.toString();
}
}