public class CommonSubstringK {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(longestCommonSubstring("leetcode", "codyabc", 3));
System.out.println(longestCommonSubstring("leetcode", "codyabc", 4));
System.out.println(longestCommonSubstring("leetcode", "codyabcetco", 4));
}
public static boolean longestCommonSubstring(String A, String B, int k) {
// write your code here
int al = A.length();
int bl = B.length();
int [][]lcs = new int[al+1][bl+1];
for (int i = 1; i <= al; i++) {
for (int j = 1; j <= bl; j++) {
if (A.charAt(i-1)==B.charAt(j-1)) {
lcs[i][j] = lcs[i-1][j-1] + 1;
if (lcs[i][j] >= k) {
return true;
}
}
}
}
return false;
}
}
common substring >= k
最新推荐文章于 2021-09-18 13:17:01 发布