public class Solution {
public static String compress (String str) {
// write code here
//1.定义返回结果,组装的变量(计算左边字符;计算右边字符;计算结果)
String result = "";
String optStr="";
String optLeftStr="";
String optRightStr="";
//2.定义本次计算的临时变量,
String optTemp = "";
String cycle = "";
String optNumberStr="";
//3.计算中间的字符串
//寻找第一个]的位置
int optRightPoint=str.indexOf("]");
optTemp = str.substring(0,optRightPoint);
int optPoint = optTemp.lastIndexOf("|");
int optPointLeft = optTemp.lastIndexOf("[");
optLeftStr = optTemp.substring(0,optPointLeft);
optRightStr = str.substring(optRightPoint+1,str.length());
optNumberStr = optTemp.substring(optPointLeft+1,optPoint);
cycle = optTemp.substring(optPoint+1,optRightPoint);
int cycleTimes = Integer.valueOf(optNumberStr);
for(int i =0 ;i < cycleTimes;i++){
optStr = optStr + cycle;
}
result= optLeftStr +optStr +optRightStr;
System.out.print("-->"+result);
if(result.contains("]")){
compress(result);
}
return result;
}
public static void main(String[] args) {
String input = "HG[3|B[2|CA]]F";
System.out.print(input);
Solution.compress(input);
// String str = "123456";
// int point = str.indexOf("3");//0开头
// String subString = str.substring(1,6);
// System.out.println(subString);
}
}