Merge pull request #3582 from shouvikbhattacharya/BAEL-1525-Check-if-a-String-is-a-palindrome
BAEL-1525: Check if a String is a palindrome
This commit is contained in:
commit
87a36777db
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.string;
|
||||
|
||||
public class Palindrome {
|
||||
|
||||
public boolean isPalindrome(String text) {
|
||||
text = text.replaceAll("\\s+", "")
|
||||
.toLowerCase();
|
||||
int length = text.length();
|
||||
int forward = 0;
|
||||
int backward = length - 1;
|
||||
boolean palindrome = true;
|
||||
while (backward > forward) {
|
||||
char forwardChar = text.charAt(forward++);
|
||||
char backwardChar = text.charAt(backward--);
|
||||
if (forwardChar != backwardChar)
|
||||
return false;
|
||||
}
|
||||
return palindrome;
|
||||
}
|
||||
|
||||
public boolean isPalindromeReverseTheString(String text) {
|
||||
String reverse = "";
|
||||
text = text.replaceAll("\\s+", "").toLowerCase();
|
||||
char[] plain = text.toCharArray();
|
||||
for (int i = plain.length - 1; i >= 0; i--)
|
||||
reverse += plain[i];
|
||||
return reverse.equals(text);
|
||||
}
|
||||
|
||||
public boolean isPalindromeUsingStringBuilder(String text) {
|
||||
StringBuilder plain = new StringBuilder(text);
|
||||
StringBuilder reverse = plain.reverse();
|
||||
return reverse.equals(plain);
|
||||
}
|
||||
|
||||
public boolean isPalindromeUsingStringBuffer(String text) {
|
||||
StringBuffer plain = new StringBuffer(text);
|
||||
StringBuffer reverse = plain.reverse();
|
||||
return reverse.equals(plain);
|
||||
}
|
||||
|
||||
public boolean isPalindromeRecursive(String text, int forward, int backward) {
|
||||
if (forward == backward)
|
||||
return true;
|
||||
if ((text.charAt(forward)) != (text.charAt(backward)))
|
||||
return false;
|
||||
if (forward < backward + 1) {
|
||||
return isPalindromeRecursive(text, forward + 1, backward - 1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
package com.baeldung.string;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class WhenCheckingPalindrome {
|
||||
|
||||
private String[] words = {
|
||||
"Anna",
|
||||
"Civic",
|
||||
"Kayak",
|
||||
"Level",
|
||||
"Madam",
|
||||
};
|
||||
|
||||
private String[] sentences = {
|
||||
"Sore was I ere I saw Eros",
|
||||
"Euston saw I was not Sue",
|
||||
"Too hot to hoot",
|
||||
"No mists or frost Simon",
|
||||
"Stella won no wallets"
|
||||
};
|
||||
|
||||
private Palindrome palindrome = new Palindrome();
|
||||
|
||||
@Test
|
||||
public void whenWord_shouldBePalindrome() {
|
||||
for(String word:words)
|
||||
Assert.assertTrue(palindrome.isPalindrome(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSentence_shouldBePalindrome() {
|
||||
for(String sentence:sentences)
|
||||
Assert.assertTrue(palindrome.isPalindrome(sentence));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseWord_shouldBePalindrome() {
|
||||
for(String word:words)
|
||||
Assert.assertTrue(palindrome.isPalindromeReverseTheString(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseSentence_shouldBePalindrome() {
|
||||
for(String sentence:sentences)
|
||||
Assert.assertTrue(palindrome.isPalindromeReverseTheString(sentence));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBuilderWord_shouldBePalindrome() {
|
||||
for(String word:words)
|
||||
Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBuilderSentence_shouldBePalindrome() {
|
||||
for(String sentence:sentences)
|
||||
Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBufferWord_shouldBePalindrome() {
|
||||
for(String word:words)
|
||||
Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(word));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringBufferSentence_shouldBePalindrome() {
|
||||
for(String sentence:sentences)
|
||||
|
||||
Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPalindromeRecursive_wordShouldBePalindrome() {
|
||||
for(String word:words) {
|
||||
word = word.replaceAll("\\s+", "").toLowerCase();
|
||||
int backward = word.length()-1;
|
||||
|
||||
Assert.assertTrue(palindrome.isPalindromeRecursive(word,0,backward));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPalindromeRecursive_sentenceShouldBePalindrome() {
|
||||
for(String sentence:sentences) {
|
||||
sentence = sentence.replaceAll("\\s+", "").toLowerCase();
|
||||
int backward = sentence.length()-1;
|
||||
|
||||
Assert.assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPalindromeStreams_wordShouldBePalindrome() {
|
||||
String[] expected = Arrays.stream(words)
|
||||
.filter(palindrome::isPalindrome)
|
||||
.toArray(String[]::new);
|
||||
|
||||
Assert.assertArrayEquals(expected, words);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPalindromeStreams_sentenceShouldBePalindrome() {
|
||||
String[] expected = Arrays.stream(sentences)
|
||||
.filter(palindrome::isPalindrome)
|
||||
.toArray(String[]::new);
|
||||
|
||||
Assert.assertArrayEquals(expected, sentences);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue