奥特曼打怪兽

打怪升级,过关斩将

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 1 package weekpratisce;
 2 
 3 ///给定两个字符串,获取两个字符串中最大相同的子串
 4 public class Demo9 {
 5     public static void main(String[] args) {
 6         String xx = "aaaaaaaaaaddddddd", yy = "45ddddda";
 7         String str = getMaxsubstring(xx, yy);
 8         System.out.println(str);
 9         // String str=xx.substring(1,10);
10         // System.out.println(str);
11     }
12     /**
13      * boolean contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true。
14      */
15 
16     // 获取两个字符串中最大相同子串。
17     /**
18      * 思路:1、将短的那个子串按照长度递减的方式获取到。 2、用长串去判断是否包含每次获取到的子串,若包含则就找到最大相同子串
19      * 
20      * @param s1
21      * @param s2
22      * @return max substring
23      */
24     public static String getMaxsubstring(String s1, String s2) {
25         String max = "";
26         String min = "";
27         // 找出最短长度
28         max = (s1.length() > s2.length()) ? s1 : s2;
29         min = (max == s1) ? s2 : s1;
30         for (int i = 0; i < min.length(); i++) {
31             for (int j = 0, k = min.length() - i; k != min.length() + 1; j++, k++) {
32                 String temp = min.substring(j, k);
33                 if (max.contains(temp)) {
34                     return temp;
35                 }
36             }
37         }
38 
39         return null;
40 
41     }
42 
43 }

 

posted on 2017-03-20 20:06  奥特曼打怪兽  阅读(975)  评论(0编辑  收藏  举报