import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return string字符串
*/
/**********************************************************************************/
// 用力通过率: 80%
/*
public String decodeString (String s) {
// write code here
Stack<Character> stack = new Stack<>();
char[] chrs = s.toCharArray();
StringBuffer sb = new StringBuffer("");
for (char chr : chrs) {
if (chr == '[') {
stack.push(chr);
}
else if (chr == ']') {
stack.pop();
StringBuffer tmpSB = new StringBuffer("");
while (!stack.isEmpty() && stack.peek() != '[') {
tmpSB.append(stack.pop());
}
tmpSB.reverse();
int num = Integer.valueOf(new String(tmpSB)); // 字符串要复制多少次
StringBuffer copySB = new StringBuffer(new String(sb));
for (int i = 1; i < num; i++) { // 复制(复制的次数要少一次,毕竟自己也算一次了嘛)
sb.append(copySB);
}
}
else if (chr >= '0' && chr <= '9') { // 如果是数字,直接入栈
stack.push(chr);
}
else { // 英文字母
sb.append(chr);
}
}
return new String(sb);
}
*/
/**********************************************************************************/
// 用力通过率: 100%
public String decodeString (String s) {
if (1 == s.length()) {
return s;
}
Stack<Integer> numStack = new Stack<>();
Stack<String> strStack = new Stack<>();
StringBuffer res = new StringBuffer("");
char[] chrs = s.toCharArray();
for (int i = 0; i < chrs.length; i++) {
if (chrs[i] >= '0' && chrs[i] <= '9') {
int num = chrs[i] - '0';
while (i + 1 < chrs.length && chrs[i + 1] >= '0' && chrs[i + 1] <= '9') {
num = num * 10 + (chrs[i + 1] - '0');
i++;
}
numStack.push(num);
}
else if (chrs[i] == '[') {
strStack.push(new String(res));
res = new StringBuffer("");
}
else if (chrs[i] == ']') {
StringBuffer tmp = new StringBuffer(strStack.pop());
int num = numStack.pop();
for (int j = 0; j < num; j++) {
tmp.append(res);
}
res = tmp;
}
else {
res.append(chrs[i]);
}
}
return new String(res);
}
}