Created
June 5, 2020 09:33
-
-
Save Desolve/94081b4e07316ddde30b741fcbea6fb8 to your computer and use it in GitHub Desktop.
1143 Longest Common Subsequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int longestCommonSubsequence(String t1, String t2) { | |
int l1 = t1.length(), l2 = t2.length(); | |
int[][] dp = new int[l1+1][l2+1]; | |
for (int i = 1; i <= l1; ++i) { | |
for (int j = 1; j <= l2; ++j) { | |
if (t1.charAt(i-1) == t2.charAt(j-1)) { | |
dp[i][j] = dp[i-1][j-1] + 1; | |
} else { | |
dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j]); | |
} | |
} | |
} | |
return dp[l1][l2]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment