Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7729ba

Browse files
Create LCS_Recurtion.java
1 parent d484809 commit e7729ba

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Recurtion Code:
2+
3+
public class LongestCommonSubsequence{
4+
5+
//recursion function
6+
public static int lcs(String a,String b , int len1, int len2)
7+
{
8+
if(len1 == 0 || len2 == 0)
9+
{
10+
return 0;
11+
}
12+
else
13+
{
14+
if(a.charAt(len1 -1 ) == b.charAt(len2 -1))
15+
{
16+
return (1+ lcs(a,b,len1-1,len2-1));
17+
}
18+
else
19+
{
20+
return Math.max(lcs(a,b,len1,len2-1) , lcs(a,b,len1-1,len2));
21+
}
22+
}
23+
}
24+
25+
26+
public static void main(String []args){
27+
String a = "ABCDGH";
28+
String b = "AEDFHR";
29+
int len1 = a.length();
30+
int len2 = b.length();
31+
int total = lcs(a,b,len1,len2);
32+
System.out.println("Length of LCS is : "+total);
33+
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /