java取两个字符串的最大交集

时间:2022-03-24 01:24:25

本文实例讲述了java取两个字符串的最大交集的实现方法,分享给大家供大家参考。具体实现方法如下:

 

复制代码代码如下:

package com.itheima.net;

 

public class Game13
{
    public static void main(String[] args)
    {
        String s1 = "135adbfg67";
        String s2 = "125dbf59";
        String s3 = s2;
        int begin = 0;
        int end = s2.length();
        int i = 1;
        while (!s1.contains(s3))
        {
            if (end == s2.length())
            {
                begin = 0;
                end = (s2.length()) - (i++);
            }
            else
            {
                begin++;end++;
            }
            s3 = s2.substring(begin, end);
            System.out.println(s3);
            System.out.println("--------");
        }
        System.out.println(s3);
    }
}

 

 

复制代码代码如下:


package com.itheima.net;

 

public class Game15
{
    public static void main(String[] args)
    {
        String s1 = "135adbfg67";
        String s2 = "125dbf59";
        method(s2, s1);
    }
    public static void method(String max, String min)
    {
        if (max.length() < min.length())
        {
            String s = max;
            max = min;
            min = s;
        }
        String subStr = min;
        for (int begin = 0, end = min.length(), i = 1; !max.contains(subStr); subStr = min.substring(begin, end))
        {
            if (end == min.length())
            {
                begin = 0;
                end = (min.length()) - (i++);
            }
            else
            {
                begin++;
                end++;
            }
            System.out.println(subStr);
            System.out.println("--------");
        }
        System.out.println(subStr);
    }
}

 

希望本文所述对大家的Java程序设计有所帮助。