字符串——最长公共子串(长度、子串)

 1     public static int longestCommonSubstring(String s1, String s2) {
 2         int len1 = s1.length();
 3         int len2 = s2.length();
 4         int result = 0;
 5         int[] index = new int[2];
 6         for(int i=0; i<len1; i++) {
 7             for(int j=0; j<len2; j++) {
 8                 int m = i;
 9                 int n = j;
10                 while(m<len1 && n<len2) {
11                     if(s1.charAt(m) == s2.charAt(n)) {
12                         m++;
13                         n++;
14                     }else {
15                         break;
16                     }
17                 }
18                 if((n-j) > result) {
19                     result = n-j;
20                     index[0] = j;
21                     index[1] = n;22                 }
23             }
24         }
25         System.out.println(s2.substring(index[0], index[1]));
26         return result;
27     }

 

转载于:https://www.cnblogs.com/xiyangchen/p/10806676.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值