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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-08 22:10:28

String 常用方法最优算法实现总结 (三) -- findCommonSubstring 和difference的相关文章

String 常用方法最优算法实现总结 (二)

1. String getOrderedString(boolean isDuplicated, String - str) 说明: Orders all characters in the input strings and return the ordered string.(note: only considering the alphabets and digits) i.e: (false, {"ahcdx", "abcuy", "cejm&qu

String常用方法

String常用方法 String s1="abc" s1是一个类类型的变量,"abc"是一个对象 字符串一旦初始化就不可以被改变 String s2=new String("abc") s1在内存中是一个对象,s2在内存中是两个对象 String覆写了Object的equals方法,比较的是两个字符串是否相等. 将此 String 与另一个 String 比较,不考虑大小写:boolean equalsIgnoreCase(str) 字符串常见

Javascript语言精粹之String常用方法分析

Javascript语言精粹之String常用方法分析 1. String常用方法分析 1.1 String.prototype.slice() slice(start,end)方法复制string的一部分来构造一个新的字符串 start<0时,它将于string.length相加 end参数可选,默认值为string.length.如果end<0,它将于string.length相加 var text='and in it he say " Any damn fool could'

JVM内存分配及String常用方法

一,JVM内存分配和常量池 ? 在介绍String类之前,先来简单分析一下在JVM中,对内存的使用是如何进行分配的.如下图所示(注意:在jdk1.8之后便没有方法区了): ? ? 如上JVM将内存分为多个不同的区域,这些区域都有各自的用途.创建和销毁的时间,有些区域随虚拟机进程的启动而存在,有些区域则是依赖用户线程的启动和结束来建立和销毁. ? 区域名称的说明: 1.1,方法区: ? 属于数据共享内存区域,存储已被虚拟机加载的类信息.常量.静态变量.即时编译器编译后的代码等数据. 1.2,虚拟机

Java 中String常用方法

java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len=s.length(); 2.charAt() 截取一个字符 例:char ch; ch="abc".charAt(1); 返回'b' 3. getChars() 截取多个字符 void getChars(int sourceStart,int sourceEnd,char target[

Java中String常用方法

java中String的常用方法1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len=s.length(); 2.charAt() 截取一个字符 例:char ch; ch="abc".charAt(1); 返回'b' 3. getChars() 截取多个字符 void getChars(int sourceStart,int sourceEnd,char target[]

js中String常用方法详解及String对象方法扩展

一.JavaScript 中 slice .substr 和 substring的区别: 1: String.slice(start,end): 一个新的字符串.包括字符串 stringObject 从 start 开始(包括 start)到 end 结束(不包括 end)为止的所有字符. 2: String.substring(start,end) 这个就有点特别了,它是先从start,end里找出一个较小的值. 然后从字符串的开始位置算起,截取较小值位置和较大值位置之间的 字符串,截取出来的

String常用方法 集锦

String的常用方法: * equals(): 判断两个String的值 是否相等 ★★★ * equalsIgnoreCase(): 判断两个String的值 是否相等 并且忽略大小写 ★★★ * * startsWith():以什么开始 * endsWith():以什么结束 * * isEmpty():判断字符串的长度是否为0 * length():计算字符串的长度 ★★★ * getBytes():★★★ * * str.substring():字符串的截取 ★★★★★ * split(

String常用方法简介

1. 创建String对象的常用方法 (1) String s1 = "mpptest" (2)  String s2 = new String(); (3) String s3 = new String("mpptest") 2. String中常用的方法,用法如图所示,具体问度娘 原文地址:https://www.cnblogs.com/mpp0905/p/10372061.html