有一个只含‘a’, ‘b‘, ‘c‘的字符串,问你这个字符串最短的、长度超过 1 的回文子串的长度是多少?

有一个只含‘a’, 'b', 'c'的字符串,问你这个字符串最短的、长度超过 1 的回文子串的长度是多少?

子串定义:字符串取一段连续的区间。例如"abcca"的子串有"ab"、"bcca"等,但"aca"则不是它的子串。
回文的定义:一个字符串正着读和倒着读都是相同的,那么定义它的回文的
 

输入

一个只包含 'a' , 'b' , 'c' 三种字符的字符串。

数据范围:字符串长度不小于2,且不超过100
 

输出

如果不存在长度超过1的回文子串,则输出-1。


否则输出长度超过1的最短回文子串的长度。

样例输入 复制

abcca

样例输出 复制

2
import java.util.Scanner;

public class Main03 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Main03 mm=new Main03();
        String zifuchuan=sc.next();
        System.out.println(mm.sum(zifuchuan));
    }
   public int sum(String chuan){
        int count=0;
       int length1=0;
        for(int i=0;i<chuan.length();i++){
            for(int j=i+2;j<=chuan.length();j++){
                StringBuffer sb=new StringBuffer(chuan.substrinchuang(i,j));
                String a1=chuan.substring(i,j);
                String a2= String.valueOf(sb.reverse());
                if(a1.equals(a2)){
                    count++;
                  int length2= a1.length();
                  if(count==1)
                      length1=length2;
                  if(length1>length2){
                      length1=length2;
                  }
                  }
                }
            }
        if(count==0){
            return -1;
        }else{
            return length1;
        }
        }

   }

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值