File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Interview Prep/section19_DynamicProgramming Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ package section19_DynamicProgramming ;
2+
3+ public class LongestCommonSubsequence {
4+
5+ public static void main (String [] args ) {
6+ String str1 = "abbg" ;
7+ String str2 = "agbg" ;
8+
9+ System .out .println (lcs (str1 , str2 ));
10+ }
11+
12+ public static int lcs (String str1 , String str2 ) {
13+ if (str1 .length () == 0 || str2 .length () == 0 )
14+ return 0 ;
15+
16+ int count = 0 ;
17+ String restString1 = str1 .substring (1 );
18+ String restString2 = str2 .substring (1 );
19+
20+ if (str1 .charAt (0 ) == str2 .charAt (0 )) {
21+ count = 1 + lcs (restString1 , restString2 );
22+ } else {
23+ int factor1 = lcs (str1 , restString2 );
24+ int factor2 = lcs (restString1 , str2 );
25+ count = Math .max (factor1 , factor2 );
26+ }
27+
28+ return count ;
29+ }
30+ }
You canβt perform that action at this time.
0 commit comments