diff --git a/core-java/src/main/java/com/baeldung/string/Palindrome.java b/core-java/src/main/java/com/baeldung/string/Palindrome.java index 0759787249..97d4d36d07 100644 --- a/core-java/src/main/java/com/baeldung/string/Palindrome.java +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -5,58 +5,62 @@ import java.util.stream.IntStream; public class Palindrome { public boolean isPalindrome(String text) { - text = text.replaceAll("\\s+", "") - .toLowerCase(); - int length = text.length(); + String clean = text.replaceAll("\\s+", "").toLowerCase(); + int length = clean.length(); int forward = 0; int backward = length - 1; - boolean palindrome = true; while (backward > forward) { - char forwardChar = text.charAt(forward++); - char backwardChar = text.charAt(backward--); + char forwardChar = clean.charAt(forward++); + char backwardChar = clean.charAt(backward--); if (forwardChar != backwardChar) return false; } - return palindrome; + return true; } public boolean isPalindromeReverseTheString(String text) { - String reverse = ""; - text = text.replaceAll("\\s+", "").toLowerCase(); - char[] plain = text.toCharArray(); + StringBuilder reverse = new StringBuilder(); + String clean = text.replaceAll("\\s+", "").toLowerCase(); + char[] plain = clean.toCharArray(); for (int i = plain.length - 1; i >= 0; i--) - reverse += plain[i]; - return reverse.equals(text); + reverse.append(plain[i]); + return (reverse.toString()).equals(clean); } public boolean isPalindromeUsingStringBuilder(String text) { - StringBuilder plain = new StringBuilder(text); + String clean = text.replaceAll("\\s+", "").toLowerCase(); + StringBuilder plain = new StringBuilder(clean); StringBuilder reverse = plain.reverse(); - return reverse.equals(plain); + return (reverse.toString()).equals(clean); } public boolean isPalindromeUsingStringBuffer(String text) { - StringBuffer plain = new StringBuffer(text); + String clean = text.replaceAll("\\s+", "").toLowerCase(); + StringBuffer plain = new StringBuffer(clean); StringBuffer reverse = plain.reverse(); - return reverse.equals(plain); + return (reverse.toString()).equals(clean); } - public boolean isPalindromeRecursive(String text, int forward, int backward) { + public boolean isPalindromeRecursive(String text) { + String clean = text.replaceAll("\\s+", "").toLowerCase(); + return recursivePalindrome(clean, 0, clean.length() - 1); + } + + private boolean recursivePalindrome(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 recursivePalindrome(text, forward + 1, backward - 1); } return true; } - + public boolean isPalindromeUsingIntStream(String text) { - String temp = text.replaceAll("\\s+", "").toLowerCase(); + String temp = text.replaceAll("\\s+", "").toLowerCase(); return IntStream.range(0, temp.length() / 2) .noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1)); } - } diff --git a/core-java/src/test/java/com/baeldung/string/PalindromeTest.java b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java index d1a05b6617..bc6fee2cd9 100644 --- a/core-java/src/test/java/com/baeldung/string/PalindromeTest.java +++ b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java @@ -25,84 +25,73 @@ public class PalindromeTest { @Test public void whenWord_shouldBePalindrome() { - for(String word:words) + for (String word : words) assertTrue(palindrome.isPalindrome(word)); } @Test public void whenSentence_shouldBePalindrome() { - for(String sentence:sentences) + for (String sentence : sentences) assertTrue(palindrome.isPalindrome(sentence)); } - + @Test public void whenReverseWord_shouldBePalindrome() { - for(String word:words) + for (String word : words) assertTrue(palindrome.isPalindromeReverseTheString(word)); } - + @Test public void whenReverseSentence_shouldBePalindrome() { - for(String sentence:sentences) + for (String sentence : sentences) assertTrue(palindrome.isPalindromeReverseTheString(sentence)); } - + @Test public void whenStringBuilderWord_shouldBePalindrome() { - for(String word:words) + for (String word : words) assertTrue(palindrome.isPalindromeUsingStringBuilder(word)); } - + @Test public void whenStringBuilderSentence_shouldBePalindrome() { - for(String sentence:sentences) + for (String sentence : sentences) assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence)); } - + @Test public void whenStringBufferWord_shouldBePalindrome() { - for(String word:words) + for (String word : words) assertTrue(palindrome.isPalindromeUsingStringBuffer(word)); } - + @Test public void whenStringBufferSentence_shouldBePalindrome() { - for(String sentence:sentences) - + for (String sentence : sentences) assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence)); } - + @Test public void whenPalindromeRecursive_wordShouldBePalindrome() { - for(String word:words) { - word = word.replaceAll("\\s+", "").toLowerCase(); - int backward = word.length()-1; - - assertTrue(palindrome.isPalindromeRecursive(word,0,backward)); - } + for (String word : words) + assertTrue(palindrome.isPalindromeRecursive(word)); } - + @Test public void whenPalindromeRecursive_sentenceShouldBePalindrome() { - for(String sentence:sentences) { - sentence = sentence.replaceAll("\\s+", "").toLowerCase(); - int backward = sentence.length()-1; - - assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward)); - } + for (String sentence : sentences) + assertTrue(palindrome.isPalindromeRecursive(sentence)); } - + @Test public void whenPalindromeStreams_wordShouldBePalindrome() { - for(String word:words) { + for (String word : words) assertTrue(palindrome.isPalindromeUsingIntStream(word)); - } } - + @Test public void whenPalindromeStreams_sentenceShouldBePalindrome() { - for(String sentence:sentences) { + for (String sentence : sentences) assertTrue(palindrome.isPalindromeUsingIntStream(sentence)); - } } }