找出字符串的最长子串,要求子串的所有字符相同,如:"abcdeeefgh"结果是"eee"

来源:互联网 发布:沉默的螺旋理论 知乎 编辑:程序博客网 时间:2024/06/12 22:45
public class TestString11 {
public static void main(String args[]){
String s = "abBBBBBBcdeeefgh";
        String result = "";
        int count = 0;
        int max = count;
        int i,j;
        for(i = 0; i < s.length(); i++){
            for(j = i + 1; j < s.length(); j++){
                if(s.charAt(i) == s.charAt(j)){
                    count++;
                }
                else
                    break;
            }
            if(count > max){
                max = count;
                result = s.substring(i,j);
            }
            count = 0;
        }
        System.out.println(result);
}
}
0 0
原创粉丝点击