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

代码如下:

/**
     * 获取两个字符串的最长重复子串
     * @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<>();
//        stringHashSet.forEach(e->{
//            stringArrayList.add(e);
//        });
//        stringHashSet1.forEach(e->{
//            stringArrayList.add(e);
//        });

        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 +
                '}';
    }
}

2.读入数据

代码如下:

data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())

该处使用的url网络请求的数据。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值