We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 3023626 + 3cfa375 commit ae02a3cCopy full SHA for ae02a3c
1 file changed
Dynamic Programming/LongestCommonSubSequence.java
@@ -0,0 +1,29 @@
1
+import java.util.*;
2
+
3
+public class LongestCommonSubSequence {
4
+ static int countlcs(String s1, String s2) {
5
+ int[][] count = new int[s1.length() + 1][s2.length() + 1];
6
+ for (int i = s1.length(); i >= 0; i--) {
7
+ for (int j = s2.length(); j >= 0; j--) {
8
+ if (i == s1.length()) {
9
+ count[i][j] = 0;
10
+ } else if (j == s2.length()) {
11
12
+ } else if (i == s1.length() && j == s2.length()) {
13
14
+ } else {
15
+ count[i][j] = s1.charAt(i) == s2.charAt(j) ? count[i + 1][j + 1] + 1
16
+ : Math.max(count[i + 1][j], count[i][j + 1]);
17
+ }
18
19
20
+ return count[0][0];
21
22
23
+ public static void main(String[] args) {
24
+ String s1 = "abcdabr";
25
+ String s2 = "aebdabr";
26
+ System.out.println(countlcs(s1, s2));
27
28
29
+}
0 commit comments