From 581e8592ae52591525054054e906f8d9df7cf9a3 Mon Sep 17 00:00:00 2001 From: shouvikbhattacharya Date: Sun, 4 Feb 2018 18:22:17 +0530 Subject: [PATCH 1/3] BAEL-1525: Article code completed. --- .../java/com/baeldung/string/Palindrome.java | 14 +++++++ .../string/WhenCheckingPalindrome.java | 37 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/string/Palindrome.java create mode 100644 core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java diff --git a/core-java/src/main/java/com/baeldung/string/Palindrome.java b/core-java/src/main/java/com/baeldung/string/Palindrome.java new file mode 100644 index 0000000000..e339d92447 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -0,0 +1,14 @@ +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)?(palindrome=(text.charAt(forward++) == text.charAt(backward--))):false); + return palindrome; + } +} diff --git a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java b/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java new file mode 100644 index 0000000000..691d74c751 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java @@ -0,0 +1,37 @@ +package com.baeldung.string; + +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)); + } +} From 6887bc10d1a3aa1d8b5778917c77c9047ef97dee Mon Sep 17 00:00:00 2001 From: shouvikbhattacharya Date: Wed, 7 Feb 2018 20:29:35 +0530 Subject: [PATCH 2/3] BAEL-1525: More changes. --- .../java/com/baeldung/string/Palindrome.java | 28 ++++++++++++++- .../string/WhenCheckingPalindrome.java | 36 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) 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 e339d92447..37284a5424 100644 --- a/core-java/src/main/java/com/baeldung/string/Palindrome.java +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -8,7 +8,33 @@ public class Palindrome { int forward = 0; int backward = length - 1; boolean palindrome = true; - while ((backward > forward)?(palindrome=(text.charAt(forward++) == text.charAt(backward--))):false); + 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.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); + } } diff --git a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java b/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java index 691d74c751..eeaed09fff 100644 --- a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java +++ b/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java @@ -34,4 +34,40 @@ public class WhenCheckingPalindrome { 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)); + } } From 9d9045f130e647ccc48f78c11a3b7980880c2a72 Mon Sep 17 00:00:00 2001 From: shouvikbhattacharya Date: Thu, 8 Feb 2018 21:04:46 +0530 Subject: [PATCH 3/3] BAEL-1525: New changes added. --- .../java/com/baeldung/string/Palindrome.java | 17 +++++++- .../string/WhenCheckingPalindrome.java | 41 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) 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 37284a5424..03b65d1a84 100644 --- a/core-java/src/main/java/com/baeldung/string/Palindrome.java +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -3,7 +3,8 @@ package com.baeldung.string; public class Palindrome { public boolean isPalindrome(String text) { - text = text.replaceAll("\\s+", "").toLowerCase(); + text = text.replaceAll("\\s+", "") + .toLowerCase(); int length = text.length(); int forward = 0; int backward = length - 1; @@ -19,7 +20,7 @@ public class Palindrome { public boolean isPalindromeReverseTheString(String text) { String reverse = ""; - text = text.toLowerCase(); + text = text.replaceAll("\\s+", "").toLowerCase(); char[] plain = text.toCharArray(); for (int i = plain.length - 1; i >= 0; i--) reverse += plain[i]; @@ -37,4 +38,16 @@ public class Palindrome { 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; + } } diff --git a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java b/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java index eeaed09fff..e3655a3140 100644 --- a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java +++ b/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java @@ -1,5 +1,7 @@ package com.baeldung.string; +import java.util.Arrays; + import org.junit.Assert; import org.junit.Test; @@ -68,6 +70,45 @@ public class WhenCheckingPalindrome { @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); + } }