给定两个字符串,获取两个字符串中最大相同的子串

 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 奥特曼打怪兽 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/fl-index/p/6591306.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值