Skip to content

Commit a19d8b2

Browse files
authored
Merge pull request TheAlgorithms#1713 from matteomessmer/LongestPalindromicSubsequence
Fixes: TheAlgorithms#1709 Longest palindromic subsequence
2 parents d7a5dbe + 60c0291 commit a19d8b2

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package test;
2+
import java.lang.*;
3+
import java.io.*;
4+
import java.util.*;
5+
6+
/**
7+
* Algorithm explanation https://www.educative.io/edpresso/longest-palindromic-subsequence-algorithm
8+
*/
9+
public class LongestPalindromicSubsequence {
10+
public static void main(String[] args) {
11+
String a = "BBABCBCAB";
12+
String b = "BABCBAB";
13+
14+
String aLPS = LPS(a);
15+
String bLPS = LPS(b);
16+
17+
System.out.println(a + " => " + aLPS);
18+
System.out.println(b + " => " + bLPS);
19+
}
20+
21+
public static String LPS(String original) throws IllegalArgumentException {
22+
StringBuilder reverse = new StringBuilder(original);
23+
reverse = reverse.reverse();
24+
return recursiveLPS(original, reverse.toString());
25+
}
26+
27+
private static String recursiveLPS(String original, String reverse) {
28+
String bestResult = "";
29+
30+
//no more chars, then return empty
31+
if(original.length() == 0 || reverse.length() == 0) {
32+
bestResult = "";
33+
} else {
34+
35+
//if the last chars match, then remove it from both strings and recur
36+
if(original.charAt(original.length() - 1) == reverse.charAt(reverse.length() - 1)) {
37+
String bestSubResult = recursiveLPS(original.substring(0, original.length() - 1), reverse.substring(0, reverse.length() - 1));
38+
39+
bestResult = reverse.charAt(reverse.length() - 1) + bestSubResult;
40+
} else {
41+
//otherwise (1) ignore the last character of reverse, and recur on original and updated reverse again
42+
//(2) ignore the last character of original and recur on the updated original and reverse again
43+
//then select the best result from these two subproblems.
44+
45+
String bestSubResult1 = recursiveLPS(original, reverse.substring(0, reverse.length() - 1));
46+
String bestSubResult2 = recursiveLPS(original.substring(0, original.length() - 1), reverse);
47+
if(bestSubResult1.length()>bestSubResult2.length()) {
48+
bestResult = bestSubResult1;
49+
} else {
50+
bestResult = bestSubResult2;
51+
}
52+
}
53+
}
54+
55+
return bestResult;
56+
}
57+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package test;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class LongestPalindromicSubsequenceTests {
8+
9+
@Test
10+
void test1() {
11+
assertEquals(LongestPalindromicSubsequence.LPS(""), "");
12+
}
13+
14+
@Test
15+
void test2() {
16+
assertEquals(LongestPalindromicSubsequence.LPS("A"), "A");
17+
}
18+
19+
@Test
20+
void test3() {
21+
assertEquals(LongestPalindromicSubsequence.LPS("BABCBAB"), "BABCBAB");
22+
}
23+
24+
@Test
25+
void test4() {
26+
assertEquals(LongestPalindromicSubsequence.LPS("BBABCBCAB"), "BABCBAB");
27+
}
28+
29+
@Test
30+
void test5() {
31+
assertEquals(LongestPalindromicSubsequence.LPS("AAAAAAAAAAAAAAAAAAAAAAAA"), "AAAAAAAAAAAAAAAAAAAAAAAA");
32+
}
33+
}

0 commit comments

Comments
 (0)