diff --git a/DynamicProgramming/LongestPalindromicSubsequence.java b/DynamicProgramming/LongestPalindromicSubsequence.java index 28b43a5f..53ea6a79 100644 --- a/DynamicProgramming/LongestPalindromicSubsequence.java +++ b/DynamicProgramming/LongestPalindromicSubsequence.java @@ -1,10 +1,9 @@ -package DynamicProgramming; +package test; import java.lang.*; import java.io.*; import java.util.*; /** - * @author Matteo Messmer https://github.com/matteomessmer * Algorithm explanation https://www.educative.io/edpresso/longest-palindromic-subsequence-algorithm */ public class LongestPalindromicSubsequence { @@ -19,9 +18,8 @@ public class LongestPalindromicSubsequence { System.out.println(b + " => " + bLPS); } - private static String LPS(String original) { - StringBuilder reverse = new StringBuilder(); - reverse.append(original); + public static String LPS(String original) throws IllegalArgumentException { + StringBuilder reverse = new StringBuilder(original); reverse = reverse.reverse(); return recursiveLPS(original, reverse.toString()); } diff --git a/DynamicProgramming/LongestPalindromicSubsequenceTests.java b/DynamicProgramming/LongestPalindromicSubsequenceTests.java new file mode 100644 index 00000000..7171f484 --- /dev/null +++ b/DynamicProgramming/LongestPalindromicSubsequenceTests.java @@ -0,0 +1,33 @@ +package test; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class LongestPalindromicSubsequenceTests { + + @Test + void test1() { + assertEquals(LongestPalindromicSubsequence.LPS(""), ""); + } + + @Test + void test2() { + assertEquals(LongestPalindromicSubsequence.LPS("A"), "A"); + } + + @Test + void test3() { + assertEquals(LongestPalindromicSubsequence.LPS("BABCBAB"), "BABCBAB"); + } + + @Test + void test4() { + assertEquals(LongestPalindromicSubsequence.LPS("BBABCBCAB"), "BABCBAB"); + } + + @Test + void test5() { + assertEquals(LongestPalindromicSubsequence.LPS("AAAAAAAAAAAAAAAAAAAAAAAA"), "AAAAAAAAAAAAAAAAAAAAAAAA"); + } +}