双节棍字符串

输入一串字符串,找出里面的最长的“双节棍”字符串

双节棍字符串   :   形如   “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;
   }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值