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

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

    }

}
 1 package com.itheima.net;

 2 

 3 public class Game15

 4 {

 5     public static void main(String[] args)

 6     {

 7         String s1 = "135adbfg67";

 8         String s2 = "125dbf59";

 9         method(s2, s1);

10     }

11     public static void method(String max, String min)

12     {

13         if (max.length() < min.length())

14         {

15             String s = max;

16             max = min;

17             min = s;

18         }

19         String subStr = min;

20         for (int begin = 0, end = min.length(), i = 1; !max.contains(subStr); subStr = min.substring(begin, end))

21         {

22             if (end == min.length())

23             {

24                 begin = 0;

25                 end = (min.length()) - (i++);

26             }

27             else

28             {

29                 begin++;

30                 end++;

31             }

32             System.out.println(subStr);

33             System.out.println("--------");

34         }

35         System.out.println(subStr);

36     }

37 }

 

你可能感兴趣的:(字符串)