lintcode最长公共子串
2018-03-14 13:34
337 查看
题目:
给出两个字符串,找到最长公共子串,并返回其长度。子串的字符应该连续的出现在原字符串中,这与子序列有所不同。样例:
给出A=“ABCD”,B=“CBCE”,返回 2答案:
public int longestCommonSubstring(String A, String B) { // write your code here if(A == null || A.length() == 0 || B == null || B.length() == 0) return 0; char[] charA = A.toCharArray(), charB = B.toCharArray(); int[][] DP = new int[charA.length + 1][charB.length + 1]; int maxLen = 0; for(int i = 1; i <= charA.length; i++) for(int j = 1; j <= charB.length; j++){ if(charA[i - 1] == charB[j - 1]) DP[i][j] = DP[i - 1][j - 1] + 1; maxLen = Math.max(maxLen, DP[i][j]); } return maxLen; }
相关文章推荐
- Lintcode—(5)最长公共子串
- Lintcode最长公共子串
- lintcode :longest common substring 最长公共子串
- LintCode : 最长公共子串
- LintCode : 最长公共子串
- LintCode:79. 最长公共子串 VS 77. 最长公共子序列
- LintCode Python 79.最长公共子串
- Lintcode(5)-最长公共子串
- [CODEVS3160]最长公共子串|后缀数组|后缀自动机
- LintCode : 最长公共前缀
- 【codevs3160】最长公共子串 后缀数组
- [POJ2774]Long Long Message [CodeVS3160]最长公共子串 做题笔记
- lintcode最长无重复字符的子串
- lintcode python ——最长公共前缀(LCP)
- lintcode ---- 最长公共前缀
- LintCode-最长回文子串
- LintCode:最长无重复字符的子串
- LintCode-最长无重复字符的子串
- lintcode---最长无重复字符的子串
- CODE【VS】 3160 最长公共子串 (后缀数组)