[LintCode] Longest Common Substring
public int longestCommonSubstring(String A, String B) { // write your code here int max = 0; int[][] dp = new int[A.length()+1][B.length()+1]; for(int i = 1 ; i <= A.length(); i++) { for(int j = 1 ; j <= B.length(); j++) { if(A.charAt(i-1) == B.charAt(j-1)) dp[i][j] = dp[i-1][j-1] + 1; else dp[i][j] = 0; max = Math.max(max,dp[i][j]); } } return max; }
Leave A Comment