前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >获取两个字符串的最长重复子串

获取两个字符串的最长重复子串

原创
作者头像
doc2
修改2024-09-25 17:41:55
700
修改2024-09-25 17:41:55
举报
文章被收录于专栏:Java技术
代码语言:txt
复制
/**
     * 获取两个字符串的最长重复子串
     * @param srcStr1
     * @param srcStr2
     * @return
     */
    public static String getMaxLenRepeatedSubStr(String srcStr1,String srcStr2){
        if (srcStr1==null){
            return null;
        }
        if (srcStr1.isEmpty()){
            return null;
        }
        if (srcStr2==null){
            return null;
        }
        if (srcStr2.isEmpty()){
            return null;
        }
        Random random = new Random();
        HashSet<String> stringHashSet = new HashSet<>();
        int count=0;
        while (true){
            int srcStr1SubStrBegIndex= random.nextInt(srcStr1.length());
            int srcStr1SubStrEndIndex= random.nextInt(srcStr1.length());
            int srcStr1SubStrEndIndex1=srcStr1SubStrEndIndex+1;
            if (srcStr1SubStrBegIndex<srcStr1SubStrEndIndex1){
                String substring = srcStr1.substring(srcStr1SubStrBegIndex, srcStr1SubStrEndIndex1);
                stringHashSet.add(substring);
            }
            if (count>1000000){
                break;
            }
            count++;
        }
        HashSet<String> stringHashSet1 = new HashSet<>();
        int count1=0;
        while (true){
            int srcStr2SubStrBegIndex= random.nextInt(srcStr2.length());
            int srcStr2SubStrEndIndex= random.nextInt(srcStr2.length());
            int srcStr2SubStrEndIndex1=srcStr2SubStrEndIndex+1;
            if (srcStr2SubStrBegIndex<srcStr2SubStrEndIndex1){
                String substring = srcStr2.substring(srcStr2SubStrBegIndex, srcStr2SubStrEndIndex1);
                stringHashSet1.add(substring);
            }
            if (count1>1000000){
                break;
            }
            count1++;
        }
        ArrayList<String> stringArrayList = new ArrayList<>();
        stringArrayList.addAll(stringHashSet);
        stringArrayList.addAll(stringHashSet1);
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        stringArrayList.forEach(e->{
            if (stringIntegerHashMap.containsKey(e)){
                stringIntegerHashMap.put(e,stringIntegerHashMap.get(e)+1);
            }else {
                stringIntegerHashMap.put(e,1);
            }
        });
        ArrayList<CustStrCompute3> stringArrayList1 = new ArrayList<>();
        stringIntegerHashMap.forEach((key,val)->{
            if (val==2){
                CustStrCompute3 custStrCompute3 = new CustStrCompute3();
                custStrCompute3.setRepeatedStr(true);
                custStrCompute3.setId(UUID.randomUUID().toString());
                custStrCompute3.setRepeatedSubStr(key);
                custStrCompute3.setRepeatedStrLen(key.length());
                stringArrayList1.add(custStrCompute3);
            }
        });
        Collections.sort(stringArrayList1, new Comparator<CustStrCompute3>() {
            @Override
            public int compare(CustStrCompute3 o1, CustStrCompute3 o2) {
                return o1.getRepeatedStrLen()-o2.getRepeatedStrLen();
            }
        });
        System.out.println(stringArrayList1);
        return stringArrayList1.get(stringArrayList1.size()-1).getRepeatedSubStr();
    }
}

class CustStrCompute3{
    private String id;
    private String repeatedSubStr;
    private Boolean isRepeatedStr;
    private Integer repeatedStrLen;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRepeatedSubStr() {
        return repeatedSubStr;
    }

    public void setRepeatedSubStr(String repeatedSubStr) {
        this.repeatedSubStr = repeatedSubStr;
    }

    public Boolean getRepeatedStr() {
        return isRepeatedStr;
    }

    public void setRepeatedStr(Boolean repeatedStr) {
        isRepeatedStr = repeatedStr;
    }

    public Integer getRepeatedStrLen() {
        return repeatedStrLen;
    }

    public void setRepeatedStrLen(Integer repeatedStrLen) {
        this.repeatedStrLen = repeatedStrLen;
    }

    @Override
    public String toString() {
        return "CustStrCompute3{" +
                "id='" + id + '\'' +
                ", repeatedSubStr='" + repeatedSubStr + '\'' +
                ", isRepeatedStr=" + isRepeatedStr +
                ", repeatedStrLen=" + repeatedStrLen +
                '}';
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档