String 常用方法最优算法实现总结 (三) -- findCommonSubstring 和difference

1. String difference(final String str1, final String str2)

说明:Compares two Strings, and returns the portion where they differ.
i.e:
("ahc", "bcu") -> "ahbu"


/**
     * 
     * @Title: difference
     * @Description: Compares two Strings, and returns the portion where they differ.
     * @param str1 input string1
     * @param str2 input string2
     * @return return the difference characters, keep the original sequence
     */
    public static String difference(final String str1, final String str2) {
        if (null == str1 || null == str2) {
            return str1 == str2 ? null : (str1 == null ? str2 : str1);
        }

        if (str1.equals(str2)) {
            return "";
        } else {
            String newStr = removeDuplicated(false, str1, str2);
            String duplicated = findDuplicated(newStr);
            final int length = duplicated.length();

            if (length == 0) {
                return newStr;
            } else {
                String rs = removeDuplicated(true, duplicated, newStr);
                return new String(rs.toCharArray(), length, rs.length() - length);
            }
        }
    }


2. List<String> findCommonSubstring(String...str)
说明:Returns the first longest substring from Strings.
i.e:
({"ahcdx", "bcuycd", "cejcdm"}) -> "cd"

/**
     * 
    * @Title: findCommonSubstring
    * @Description: Returns the first longest substring from Strings.
    * 
    * @param strArr input string array
    * @return the repeated Stringc
     */
    public static List<String> findCommonSubstring(String... strArr) {
        /* initial check */
        if (strArr == null || strArr.length == 0) {
            return null;
        } else if (strArr.length == 1) {
            return Arrays.asList(strArr);
        }

        /* check the first element */
        if (isEmpty(strArr[0])) {
            return null;
        }

        int smallLen = strArr[0].length();
        int index = 0;
        int len = strArr.length;

        /* identify smallest String */
        for (int i = 1; i < len; i++) {
            /* if contains empty, returns directly */
            if (isEmpty(strArr[i])) {
                return null;
            }

            if (smallLen > strArr[i].length()) {
                smallLen = strArr[i].length();
                index = i;
            }
        }

        String smallStr = strArr[index];
        List<String> list = new ArrayList<String>();
        int step = 1;

        /* outer loop smallest string */
        for (int i = 0; i < smallLen; i++) {
            boolean isPass = true;

            /* inner loop smallest string */
            for (int j = i + step; j <= smallLen; j++) {
                String tempCom = smallStr.substring(i, j);

                /*
                 * loop and judge whether temp string is contained in other strings
                 */
                for (int k = 0; k < len; k++) {
                    if (k != index && !strArr[k].contains(tempCom)) {
                        isPass = false;
                        break;
                    }
                }

                /* if not contained, terminal the loop */
                if (!isPass) {
                    break;
                }

                if (list.isEmpty()) {
                    list.add(tempCom);
                } else {
                    int maxLen = list.get(0).length();
                    step = j - i;

                    /* if existing longer string, clear the list */
                    if (maxLen < step) {
                        list.clear();
                    }

                    /* if existing same and longer string, add it to list */
                    if (maxLen <= step && !list.contains(tempCom)) {
                        list.add(tempCom);
                    }
                }
            }
        }

        return list;
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值