题解 | #最长公共子串#
最长公共子串
http://www.nowcoder.com/practice/f33f5adc55f444baa0e0ca87ad8a6aac
滑动窗口[start, end),如果符合,就扩充,否则移动窗口start++
import java.util.*;
public class Solution {
/**
* longest common substring
* @param str1 string字符串 the string
* @param str2 string字符串 the string
* @return string字符串
*/
public String LCS (String text1, String text2) {
// write code here
int start = 0;
int end = 1;
String ans = "";
while(end <= text2.length()) {
String sub = text2.substring(start, end);
if (text1.contains(sub)) {
ans = sub;
} else{
start++;
}
end++;
}
return ans;
}
}