Skip to content

Commit 5072291

Browse files
PranayVJainKevinGilmore
authored andcommitted
BAEL-2337: Find Substring which are palindromes (eugenp#5766)
* BAEL-2337: Added test cases to find substring which are palindromes * BAEL-2337: Changed the names of the variables * BAEL-2237: Formatted code using formatter.xml * BAEL-2237: Renamed method and added variety of test cases * BAEL-2337: Renamed unit test cases name according to BBD style naming convention
1 parent 2338f29 commit 5072291

2 files changed

Lines changed: 173 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.baeldung.string;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class SubstringPalindrome {
7+
8+
public Set<String> findAllPalindromesUsingCenter(String input) {
9+
final Set<String> palindromes = new HashSet<>();
10+
if (input == null || input.isEmpty()) {
11+
return palindromes;
12+
}
13+
if (input.length() == 1) {
14+
palindromes.add(input);
15+
return palindromes;
16+
}
17+
for (int i = 0; i < input.length(); i++) {
18+
palindromes.addAll(findPalindromes(input, i, i + 1));
19+
palindromes.addAll(findPalindromes(input, i, i));
20+
}
21+
return palindromes;
22+
}
23+
24+
private Set<String> findPalindromes(String input, int low, int high) {
25+
Set<String> result = new HashSet<>();
26+
while (low >= 0 && high < input.length() && input.charAt(low) == input.charAt(high)) {
27+
result.add(input.substring(low, high + 1));
28+
low--;
29+
high++;
30+
}
31+
return result;
32+
}
33+
34+
public Set<String> findAllPalindromesUsingBruteForceApproach(String input) {
35+
Set<String> palindromes = new HashSet<>();
36+
if (input == null || input.isEmpty()) {
37+
return palindromes;
38+
}
39+
if (input.length() == 1) {
40+
palindromes.add(input);
41+
return palindromes;
42+
}
43+
for (int i = 0; i < input.length(); i++) {
44+
for (int j = i + 1; j <= input.length(); j++)
45+
if (isPalindrome(input.substring(i, j))) {
46+
palindromes.add(input.substring(i, j));
47+
}
48+
}
49+
return palindromes;
50+
}
51+
52+
private boolean isPalindrome(String input) {
53+
StringBuilder plain = new StringBuilder(input);
54+
StringBuilder reverse = plain.reverse();
55+
return (reverse.toString()).equals(input);
56+
}
57+
58+
public Set<String> findAllPalindromesUsingManachersAlgorithm(String input) {
59+
Set<String> palindromes = new HashSet<>();
60+
String formattedInput = "@" + input + "#";
61+
char inputCharArr[] = formattedInput.toCharArray();
62+
int max;
63+
int radius[][] = new int[2][input.length() + 1];
64+
for (int j = 0; j <= 1; j++) {
65+
radius[j][0] = max = 0;
66+
int i = 1;
67+
while (i <= input.length()) {
68+
palindromes.add(Character.toString(inputCharArr[i]));
69+
while (inputCharArr[i - max - 1] == inputCharArr[i + j + max])
70+
max++;
71+
radius[j][i] = max;
72+
int k = 1;
73+
while ((radius[j][i - k] != max - k) && (k < max)) {
74+
radius[j][i + k] = Math.min(radius[j][i - k], max - k);
75+
k++;
76+
}
77+
max = Math.max(max - k, 0);
78+
i += k;
79+
}
80+
}
81+
for (int i = 1; i <= input.length(); i++) {
82+
for (int j = 0; j <= 1; j++) {
83+
for (max = radius[j][i]; max > 0; max--) {
84+
palindromes.add(input.substring(i - max - 1, max + j + i - 1));
85+
}
86+
}
87+
}
88+
return palindromes;
89+
}
90+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.string;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
import org.junit.Test;
7+
8+
public class SubstringPalindromeUnitTest {
9+
10+
private static final String INPUT_BUBBLE = "bubble";
11+
private static final String INPUT_CIVIC = "civic";
12+
private static final String INPUT_INDEED = "indeed";
13+
private static final String INPUT_ABABAC = "ababac";
14+
15+
Set<String> EXPECTED_PALINDROME_BUBBLE = new HashSet<String>() {
16+
{
17+
add("b");
18+
add("u");
19+
add("l");
20+
add("e");
21+
add("bb");
22+
add("bub");
23+
}
24+
};
25+
26+
Set<String> EXPECTED_PALINDROME_CIVIC = new HashSet<String>() {
27+
{
28+
add("civic");
29+
add("ivi");
30+
add("i");
31+
add("c");
32+
add("v");
33+
}
34+
};
35+
36+
Set<String> EXPECTED_PALINDROME_INDEED = new HashSet<String>() {
37+
{
38+
add("i");
39+
add("n");
40+
add("d");
41+
add("e");
42+
add("ee");
43+
add("deed");
44+
}
45+
};
46+
47+
Set<String> EXPECTED_PALINDROME_ABABAC = new HashSet<String>() {
48+
{
49+
add("a");
50+
add("b");
51+
add("c");
52+
add("aba");
53+
add("bab");
54+
add("ababa");
55+
}
56+
};
57+
58+
private SubstringPalindrome palindrome = new SubstringPalindrome();
59+
60+
@Test
61+
public void whenUsingManachersAlgorithm_thenFindsAllPalindromes() {
62+
assertEquals(EXPECTED_PALINDROME_BUBBLE, palindrome.findAllPalindromesUsingManachersAlgorithm(INPUT_BUBBLE));
63+
assertEquals(EXPECTED_PALINDROME_INDEED, palindrome.findAllPalindromesUsingManachersAlgorithm(INPUT_INDEED));
64+
assertEquals(EXPECTED_PALINDROME_CIVIC, palindrome.findAllPalindromesUsingManachersAlgorithm(INPUT_CIVIC));
65+
assertEquals(EXPECTED_PALINDROME_ABABAC, palindrome.findAllPalindromesUsingManachersAlgorithm(INPUT_ABABAC));
66+
}
67+
68+
@Test
69+
public void whenUsingCenterApproach_thenFindsAllPalindromes() {
70+
assertEquals(EXPECTED_PALINDROME_BUBBLE, palindrome.findAllPalindromesUsingCenter(INPUT_BUBBLE));
71+
assertEquals(EXPECTED_PALINDROME_INDEED, palindrome.findAllPalindromesUsingCenter(INPUT_INDEED));
72+
assertEquals(EXPECTED_PALINDROME_CIVIC, palindrome.findAllPalindromesUsingCenter(INPUT_CIVIC));
73+
assertEquals(EXPECTED_PALINDROME_ABABAC, palindrome.findAllPalindromesUsingCenter(INPUT_ABABAC));
74+
}
75+
76+
@Test
77+
public void whenUsingBruteForceApproach_thenFindsAllPalindromes() {
78+
assertEquals(EXPECTED_PALINDROME_BUBBLE, palindrome.findAllPalindromesUsingBruteForceApproach(INPUT_BUBBLE));
79+
assertEquals(EXPECTED_PALINDROME_INDEED, palindrome.findAllPalindromesUsingBruteForceApproach(INPUT_INDEED));
80+
assertEquals(EXPECTED_PALINDROME_CIVIC, palindrome.findAllPalindromesUsingBruteForceApproach(INPUT_CIVIC));
81+
assertEquals(EXPECTED_PALINDROME_ABABAC, palindrome.findAllPalindromesUsingBruteForceApproach(INPUT_ABABAC));
82+
}
83+
}

0 commit comments

Comments
 (0)