Skip to content

Commit 37535bc

Browse files
committed
fix: formatting and style fixes for clang-format compliance
1 parent c63ced8 commit 37535bc

2 files changed

Lines changed: 24 additions & 55 deletions

File tree

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
package com.thealgorithms.dynamicprogramming;
2-
2+
33
/**
4-
* Longest Palindromic Subsequence using dynamic programming via LCS.
4+
* Longest Palindromic Subsequence algorithm.
5+
* A palindromic subsequence is a subsequence that reads the same forwards and backwards.
6+
* This implementation finds the longest such subsequence by computing the LCS of the
7+
* original string and its reverse.
58
*
6-
* <p>A palindromic subsequence is a subsequence that reads the same forwards
7-
* and backwards. This implementation finds the longest such subsequence by
8-
* computing the LCS of the original string and its reverse.
9-
*
10-
* <p>Time Complexity: O(2^n) — recursive without memoization.
11-
* Space Complexity: O(n) — recursion stack depth.
12-
*
13-
* @see <a href="https://www.educative.io/edpresso/longest-palindromic-subsequence-algorithm">
14-
* Algorithm explanation</a>
9+
* @see <a href="https://en.wikipedia.org/wiki/Longest_palindromic_subsequence">Wikipedia</a>
1510
*/
1611
public final class LongestPalindromicSubsequence {
17-
1812
private LongestPalindromicSubsequence() {
1913
}
20-
14+
2115
/**
2216
* Returns the longest palindromic subsequence of the given string.
2317
*
@@ -32,22 +26,17 @@ public static String lps(String original) {
3226
String reverse = new StringBuilder(original).reverse().toString();
3327
return recursiveLPS(original, reverse);
3428
}
35-
29+
3630
private static String recursiveLPS(String original, String reverse) {
3731
if (original.isEmpty() || reverse.isEmpty()) {
3832
return "";
3933
}
40-
4134
if (original.charAt(original.length() - 1) == reverse.charAt(reverse.length() - 1)) {
42-
String bestSubResult = recursiveLPS(
43-
original.substring(0, original.length() - 1),
44-
reverse.substring(0, reverse.length() - 1)
45-
);
35+
String bestSubResult = recursiveLPS(original.substring(0, original.length() - 1), reverse.substring(0, reverse.length() - 1));
4636
return reverse.charAt(reverse.length() - 1) + bestSubResult;
4737
}
48-
4938
String sub1 = recursiveLPS(original, reverse.substring(0, reverse.length() - 1));
5039
String sub2 = recursiveLPS(original.substring(0, original.length() - 1), reverse);
5140
return sub1.length() >= sub2.length() ? sub1 : sub2;
5241
}
53-
}
42+
}
Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,54 @@
11
package com.thealgorithms.dynamicprogramming;
2-
2+
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
5-
5+
66
import org.junit.jupiter.api.Test;
77
import org.junit.jupiter.params.ParameterizedTest;
88
import org.junit.jupiter.params.provider.CsvSource;
9-
9+
1010
public class LongestPalindromicSubsequenceTest {
11-
11+
1212
@ParameterizedTest
13-
@CsvSource({
14-
"BBABCBCAB, BABCBAB",
15-
"BABCBAB, BABCBAB",
16-
"A, A",
17-
"AA, AA",
18-
"AB, A",
19-
})
13+
@CsvSource({"BBABCBCAB, BABCBAB", "BABCBAB, BABCBAB", "A, A", "AA, AA", "AB, A"})
2014
void testLpsKnownCases(String input, String expectedLps) {
21-
assertEquals(expectedLps.strip(), LongestPalindromicSubsequence.lps(input.strip()));
15+
assertEquals(expectedLps, LongestPalindromicSubsequence.lps(input));
2216
}
23-
17+
2418
@Test
2519
void testLpsEmptyString() {
2620
assertEquals("", LongestPalindromicSubsequence.lps(""));
2721
}
28-
22+
2923
@Test
3024
void testLpsSingleCharacter() {
3125
assertEquals("Z", LongestPalindromicSubsequence.lps("Z"));
3226
}
33-
27+
3428
@Test
3529
void testLpsAllSameCharacters() {
3630
assertEquals("AAAA", LongestPalindromicSubsequence.lps("AAAA"));
3731
}
38-
32+
3933
@Test
4034
void testLpsAlreadyPalindrome() {
4135
assertEquals("RACECAR", LongestPalindromicSubsequence.lps("RACECAR"));
4236
}
43-
37+
4438
@Test
4539
void testLpsNoRepeatingCharacters() {
46-
// Only one character can form a palindrome when all chars are unique
4740
assertEquals(1, LongestPalindromicSubsequence.lps("ABCDE").length());
4841
}
49-
42+
5043
@Test
5144
void testLpsNullThrowsException() {
52-
assertThrows(IllegalArgumentException.class,
53-
() -> LongestPalindromicSubsequence.lps(null));
45+
assertThrows(IllegalArgumentException.class, () -> LongestPalindromicSubsequence.lps(null));
5446
}
55-
47+
5648
@Test
5749
void testLpsResultIsActuallyPalindrome() {
5850
String result = LongestPalindromicSubsequence.lps("BBABCBCAB");
5951
String reversed = new StringBuilder(result).reverse().toString();
6052
assertEquals(result, reversed);
6153
}
62-
63-
@Test
64-
void testLpsResultIsSubsequenceOfInput() {
65-
String input = "BBABCBCAB";
66-
String result = LongestPalindromicSubsequence.lps(input);
67-
int idx = 0;
68-
for (char c : result.toCharArray()) {
69-
idx = input.indexOf(c, idx);
70-
assert idx != -1 : "Result character not found in input at expected position";
71-
idx++;
72-
}
73-
}
7454
}

0 commit comments

Comments
 (0)