美文网首页
找出最长重复字串

找出最长重复字串

作者: 小圆圈Belen | 来源:发表于2020-11-11 14:27 被阅读0次

本人写的只是能实现的一种方式,并没有达到最优解

public class MaxLength {
    public static void Maxlen(String src){
        if (src == null || src.length() < 1){
            return;
        }
        char[] chars = src.toCharArray();
        int max = 0;
        int size = 0;
        int start = 0;
        int end = 0;
        String maxStr = null;
        for(int i = 0; i<chars.length; i++){
            size = 0;
            lable:
            for(int j = i+1; j<chars.length-1; j++){
                if(chars[i]==chars[j]){
                    start = i;
                    end = j;
                    size = end-start+1;
                    break lable;
                }
            }
            if(size>max){
                max = size;
                maxStr = src.substring(start,end);
            }
        }
       // 这个判断主要是为了防止整个字符串没有重复字串
        if(maxStr==null&&size==0){
            maxStr = src;
        }
        System.out.println(maxStr);
    }
    public static void main(String [] args){
        String src = "qwertyu";
        Maxlen(src);
    }
}

相关文章

网友评论

      本文标题:找出最长重复字串

      本文链接:https://www.haomeiwen.com/subject/khzdbktx.html