Skip to content

Instantly share code, notes, and snippets.

@Desolve
Created June 5, 2020 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Desolve/94081b4e07316ddde30b741fcbea6fb8 to your computer and use it in GitHub Desktop.
Save Desolve/94081b4e07316ddde30b741fcbea6fb8 to your computer and use it in GitHub Desktop.
1143 Longest Common Subsequence
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