判断字符串数组是否有相同的最长字符串前缀


 /**
     * 判断字符串数组是否有相同的最长字符串前缀
     * @param strArr
     * @return
     */
    public static String getSamePrefixSubStr(String[] strArr){
        if (strArr==null){
            return null;
        }
        if (strArr.length==0){
            return null;
        }

        Random random = new Random();
        LinkedHashSet<String> stringLinkedHashSet = new LinkedHashSet<>();
        for (int i = 0; i < strArr.length; i++) {
            String s = strArr[i];
            int count=0;
            while (true){
                int prefixSubStrBegIndex=0;
                int prefixSubStrEndIndex=random.nextInt(s.length());
                int prefixSubStrEndIndex2=prefixSubStrEndIndex+1;
                String substring = s.substring(prefixSubStrBegIndex, prefixSubStrEndIndex2);
                stringLinkedHashSet.add(substring);
                if (count>1000000){
                    break;
                }
                count++;
            }
        }

        System.out.println(stringLinkedHashSet);

        ArrayList<String> stringArrayList1 = new ArrayList<>();
        for (String s : stringLinkedHashSet) {
            stringArrayList1.add(s);
        }

        int count=0;
        int j=0;
        ArrayList<CustStrCompute2> custStrCompute2s = new ArrayList<>();
        for (int i = 0; i < strArr.length; i++) {
            String s = strArr[i];
            String s1 = stringArrayList1.get(j);
            if (s.contains(s1)){
                count++;
                j=0;
                if (count==strArr.length){
                    CustStrCompute2 custStrCompute2 = new CustStrCompute2();
                    custStrCompute2.setId(UUID.randomUUID().toString());
                    custStrCompute2.setSubStrPrefix(s1);
                    custStrCompute2.setStrPrefix(true);
                    custStrCompute2.setRepeandPrefixStr(true);
                    custStrCompute2.setPrefixStrLen(s1.length());
                    custStrCompute2s.add(custStrCompute2);
                    i=0;
                    j++;
                    count=0;
                    continue;
                }
                continue;
            }else {
                i=0;
                j++;
                count=0;
            }

        }

        Collections.sort(custStrCompute2s, new Comparator<CustStrCompute2>() {
            @Override
            public int compare(CustStrCompute2 o1, CustStrCompute2 o2) {
                return o1.getPrefixStrLen()-o2.getPrefixStrLen();
            }
        });

        System.out.println(custStrCompute2s);
        return custStrCompute2s.get(custStrCompute2s.size()-1).getSubStrPrefix();
    }

class CustStrCompute2{
    private String id;
    private String subStrPrefix;
    private Boolean isStrPrefix;
    private Boolean isRepeandPrefixStr;
    private Integer prefixStrLen;

    public String getId() {
        return id;
    }

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

    public String getSubStrPrefix() {
        return subStrPrefix;
    }

    public void setSubStrPrefix(String subStrPrefix) {
        this.subStrPrefix = subStrPrefix;
    }

    public Boolean getStrPrefix() {
        return isStrPrefix;
    }

    public void setStrPrefix(Boolean strPrefix) {
        isStrPrefix = strPrefix;
    }

    public Boolean getRepeandPrefixStr() {
        return isRepeandPrefixStr;
    }

    public void setRepeandPrefixStr(Boolean repeandPrefixStr) {
        isRepeandPrefixStr = repeandPrefixStr;
    }

    public Integer getPrefixStrLen() {
        return prefixStrLen;
    }

    public void setPrefixStrLen(Integer prefixStrLen) {
        this.prefixStrLen = prefixStrLen;
    }

    @Override
    public String toString() {
        return "CustStrCompute2{" +
                "id='" + id + '\'' +
                ", subStrPrefix='" + subStrPrefix + '\'' +
                ", isStrPrefix=" + isStrPrefix +
                ", isRepeandPrefixStr=" + isRepeandPrefixStr +
                ", prefixStrLen=" + prefixStrLen +
                '}';
    }
}
#我的实习求职记录#
Java技术 文章被收录于专栏

JavaEE技术 编程开发经验 企业通用技术

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务