输入一串字符串,找出里面的最长的“双节棍”字符串
双节棍字符串 : 形如 “aaa....bb....aaa” 左右两端为相同的字符,且数量相等,中间为另一重复的字符串
比如 输入 AAABDDDKDDDL 输出DDDKDDD
public static String get(String s){
char[] c=s.toCharArray();
String result=null;
int len=c.length;
int i=0;
int j=0;
int count1=1;
int count2=1;
int count3=1;
char temp1=0;
char temp2=0;
Stack<Character> stack0=new Stack<>();
while(i<len-1){
if(c[i]!=c[i+1]){
i++;
continue;
}
stack0.push(c[i]);
while(i<len-1&&c[i]==c[i+1]){
temp1=c[i];
stack0.push(c[i+1]);
i++;
count1++;
}
i++;
if(i>=len)
break;
j=i;
stack0.push(c[i]);
while(i<len-1&&c[i]==c[i+1]){
stack0.push(c[i+1]);
i++;
count2++;
}
i++;
if(i>=len)
break;
if(count2>=count1){
stack0=new Stack<>();
i=j;
count1=1;
count2=1;
count3=1;
continue;
}
stack0.push(c[i]);
temp2=c[i];
while(i<len-1&&c[i]==c[i+1]){
stack0.push(c[i+1]);
i++;
count3++;
}
if(temp1==temp2&&count1==count3){
char[] cc=new char[stack0.size()];
int k=0;
while(stack0.size()>0){
cc[k]=stack0.pop();
k++;
}
String str=new String(cc);
if(result==null||result.length()<str.length()){
result=str;
}
}
stack0=new Stack<>();
i=j;
count1=1;
count2=1;
count3=1;
}
return result;
}