Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/java/com/github/pedrovgs/Longest_Palindrome_Subsequence
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class Longest_Palindrome_Subsequence {
static int max(int x, int y) {
if(x>y)
return x;
else
return y;
}
static int lps(char s[], int i, int j) {

if (i == j) {
return 1;
}
if (s[i] == s[j] && i + 1 == j) {
return 2;
}
if (s[i] == s[j]) {
return lps(s, i + 1, j - 1) + 2;
}
return max(lps(s, i, j - 1), lps(s, i + 1, j));
}
public static void main(String[] args) {
String s = "ABBDCACBAD";
int n = s.length();
System.out.print("The longest palindrome subsequence is "+l
ps(s.toCharArray(), 0, n - 1));
}
}