From 438e9dc4185cf3abf022fa6387123ce9dd9c8ab4 Mon Sep 17 00:00:00 2001 From: jotdeep <59995823+jotdeep@users.noreply.github.com> Date: Sun, 3 Oct 2021 11:47:55 +0530 Subject: [PATCH] Added Longest Common Subsequence --- LongestCommonSubsequence.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 LongestCommonSubsequence.java diff --git a/LongestCommonSubsequence.java b/LongestCommonSubsequence.java new file mode 100644 index 0000000..ed7b2e4 --- /dev/null +++ b/LongestCommonSubsequence.java @@ -0,0 +1,29 @@ +import java.util.*; + +class Solution8 { + public int solve(String a, String b) { + int dp[][]=new int[a.length()][b.length()]; + for (int i=0;i<=a.length();i++) + { + for (int j=0;j<=b.length();j++) + { + + if (i==0||j==0) + { + dp[i][j]=0; + } + else + { + if (a.charAt(i)==b.charAt(j)) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } + else + { + dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]); + } + } + } + } + return dp[a.length()][b.length()]; + } +} \ No newline at end of file