Skip to content

Commit 87a3677

Browse files
authored
Merge pull request eugenp#3582 from shouvikbhattacharya/BAEL-1525-Check-if-a-String-is-a-palindrome
BAEL-1525: Check if a String is a palindrome
2 parents 1fb5890 + 830042c commit 87a3677

2 files changed

Lines changed: 167 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.string;
2+
3+
public class Palindrome {
4+
5+
public boolean isPalindrome(String text) {
6+
text = text.replaceAll("\\s+", "")
7+
.toLowerCase();
8+
int length = text.length();
9+
int forward = 0;
10+
int backward = length - 1;
11+
boolean palindrome = true;
12+
while (backward > forward) {
13+
char forwardChar = text.charAt(forward++);
14+
char backwardChar = text.charAt(backward--);
15+
if (forwardChar != backwardChar)
16+
return false;
17+
}
18+
return palindrome;
19+
}
20+
21+
public boolean isPalindromeReverseTheString(String text) {
22+
String reverse = "";
23+
text = text.replaceAll("\\s+", "").toLowerCase();
24+
char[] plain = text.toCharArray();
25+
for (int i = plain.length - 1; i >= 0; i--)
26+
reverse += plain[i];
27+
return reverse.equals(text);
28+
}
29+
30+
public boolean isPalindromeUsingStringBuilder(String text) {
31+
StringBuilder plain = new StringBuilder(text);
32+
StringBuilder reverse = plain.reverse();
33+
return reverse.equals(plain);
34+
}
35+
36+
public boolean isPalindromeUsingStringBuffer(String text) {
37+
StringBuffer plain = new StringBuffer(text);
38+
StringBuffer reverse = plain.reverse();
39+
return reverse.equals(plain);
40+
}
41+
42+
public boolean isPalindromeRecursive(String text, int forward, int backward) {
43+
if (forward == backward)
44+
return true;
45+
if ((text.charAt(forward)) != (text.charAt(backward)))
46+
return false;
47+
if (forward < backward + 1) {
48+
return isPalindromeRecursive(text, forward + 1, backward - 1);
49+
}
50+
51+
return true;
52+
}
53+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.baeldung.string;
2+
3+
import java.util.Arrays;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
public class WhenCheckingPalindrome {
9+
10+
private String[] words = {
11+
"Anna",
12+
"Civic",
13+
"Kayak",
14+
"Level",
15+
"Madam",
16+
};
17+
18+
private String[] sentences = {
19+
"Sore was I ere I saw Eros",
20+
"Euston saw I was not Sue",
21+
"Too hot to hoot",
22+
"No mists or frost Simon",
23+
"Stella won no wallets"
24+
};
25+
26+
private Palindrome palindrome = new Palindrome();
27+
28+
@Test
29+
public void whenWord_shouldBePalindrome() {
30+
for(String word:words)
31+
Assert.assertTrue(palindrome.isPalindrome(word));
32+
}
33+
34+
@Test
35+
public void whenSentence_shouldBePalindrome() {
36+
for(String sentence:sentences)
37+
Assert.assertTrue(palindrome.isPalindrome(sentence));
38+
}
39+
40+
@Test
41+
public void whenReverseWord_shouldBePalindrome() {
42+
for(String word:words)
43+
Assert.assertTrue(palindrome.isPalindromeReverseTheString(word));
44+
}
45+
46+
@Test
47+
public void whenReverseSentence_shouldBePalindrome() {
48+
for(String sentence:sentences)
49+
Assert.assertTrue(palindrome.isPalindromeReverseTheString(sentence));
50+
}
51+
52+
@Test
53+
public void whenStringBuilderWord_shouldBePalindrome() {
54+
for(String word:words)
55+
Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(word));
56+
}
57+
58+
@Test
59+
public void whenStringBuilderSentence_shouldBePalindrome() {
60+
for(String sentence:sentences)
61+
Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence));
62+
}
63+
64+
@Test
65+
public void whenStringBufferWord_shouldBePalindrome() {
66+
for(String word:words)
67+
Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(word));
68+
}
69+
70+
@Test
71+
public void whenStringBufferSentence_shouldBePalindrome() {
72+
for(String sentence:sentences)
73+
74+
Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence));
75+
}
76+
77+
@Test
78+
public void whenPalindromeRecursive_wordShouldBePalindrome() {
79+
for(String word:words) {
80+
word = word.replaceAll("\\s+", "").toLowerCase();
81+
int backward = word.length()-1;
82+
83+
Assert.assertTrue(palindrome.isPalindromeRecursive(word,0,backward));
84+
}
85+
}
86+
87+
@Test
88+
public void whenPalindromeRecursive_sentenceShouldBePalindrome() {
89+
for(String sentence:sentences) {
90+
sentence = sentence.replaceAll("\\s+", "").toLowerCase();
91+
int backward = sentence.length()-1;
92+
93+
Assert.assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward));
94+
}
95+
}
96+
97+
@Test
98+
public void whenPalindromeStreams_wordShouldBePalindrome() {
99+
String[] expected = Arrays.stream(words)
100+
.filter(palindrome::isPalindrome)
101+
.toArray(String[]::new);
102+
103+
Assert.assertArrayEquals(expected, words);
104+
}
105+
106+
@Test
107+
public void whenPalindromeStreams_sentenceShouldBePalindrome() {
108+
String[] expected = Arrays.stream(sentences)
109+
.filter(palindrome::isPalindrome)
110+
.toArray(String[]::new);
111+
112+
Assert.assertArrayEquals(expected, sentences);
113+
}
114+
}

0 commit comments

Comments
 (0)