forked from zapellass123/OpenEmailGenerator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLongestCommonSubsequence.java
More file actions
24 lines (21 loc) · 1.17 KB
/
LongestCommonSubsequence.java
File metadata and controls
24 lines (21 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Given two strings, determine the length of the longest common subsequence which is common in both the strings
public class LongestCommonSubsequence{
public static void main(String[] args) {
String firstString= "ACEDBFD";
String secondString= "AECDF";
int length_firstString = firstString.length();
int length_secondString = secondString.length();
System.out.println("Length of Longest Common Subsequence : "+ longestCommonSubsequence(firstString.toCharArray(), secondString.toCharArray(), length_firstString, length_secondString));
}
static int longestCommonSubsequence(char[] firstString, char[] secondString, int length_firstString, int length_secondString){
if(length_firstString<=0 || length_secondString<=0)
return 0;
if(firstString[length_firstString-1] == secondString[length_secondString-1]){
return 1 + longestCommonSubsequence(firstString, secondString, length_firstString-1, length_secondString-1);
}
return Math.max(
longestCommonSubsequence(firstString, secondString, length_firstString, length_secondString-1),
longestCommonSubsequence(firstString,secondString, length_firstString-1,length_secondString)
);
}
}