LintCode-Longest Common Substring
2014-12-31 23:13
357 查看
Given two strings, find the longest common substring.
Return the length of it.
Note
The characters in substring should occur continiously in original string. This is different with subsequnce.
Solution:
Return the length of it.
Note
The characters in substring should occur continiously in original string. This is different with subsequnce.
Solution:
public class Solution { /** * @param A, B: Two string. * @return: the length of the longest common substring. */ public int longestCommonSubstring(String A, String B) { int lenA = A.length(); int lenB = B.length(); if (lenA==0 || lenB ==0 ) return 0; int[][] lcs = new int[lenA+1][lenB+1]; for (int i=0;i<=lenA;i++) lcs[i][0] = 0; for (int i=0;i<=lenB;i++) lcs[0][i] = 0; int res = 0; for (int i=1;i<=lenA;i++) for (int j=1;j<=lenB;j++) if (A.charAt(i-1)==B.charAt(j-1)){ lcs[i][j]=lcs[i-1][j-1]+1; if (lcs[i][j]>res) res = lcs[i][j]; } else lcs[i][j]=0; return res; } }
相关文章推荐
- LintCode Longest Common Substring
- lintcode :longest common substring 最长公共子串
- lintcode:Longest Common Substring
- LintCode Longest Common Substring
- lintcode:Longest Substring with At Most K Distinct Characters
- LintCode Longest Common Subsequence
- LintCode:M-Longest Palindromic Substring
- Lintcode - Longest common subsequence
- lintcode-medium-Longest Common Prefix
- LintCode Longest Common Subsequence(最长公共子序列长度,动态规划入门题)
- lintcode 中等题:Longest Palindromic Substring 最长回文子串
- LintCode : 最长回文子字符串 Longest Palindromic Substring
- lintcode: Longest Common Prefix
- Lintcode: Longest Common Subsequence
- lintcode longest-common-subsequence 最长公共子序列 证明
- LintCode_200 Longest Palindromic Substring
- HDOJ 1403:Longest Common Substring 后缀数组的用处
- 后缀数组:HDU1043 Longest Common Substring
- SPOJ 1812 Longest Common Substring II(后缀自动机)
- LEEDCODE 14 Longest Common Prefix (JAVA题解)