BAEL-1525: New changes added.

This commit is contained in:
shouvikbhattacharya 2018-02-08 21:04:46 +05:30
parent 15ea8a7c01
commit 9d9045f130
2 changed files with 56 additions and 2 deletions

View File

@ -3,7 +3,8 @@ package com.baeldung.string;
public class Palindrome { public class Palindrome {
public boolean isPalindrome(String text) { public boolean isPalindrome(String text) {
text = text.replaceAll("\\s+", "").toLowerCase(); text = text.replaceAll("\\s+", "")
.toLowerCase();
int length = text.length(); int length = text.length();
int forward = 0; int forward = 0;
int backward = length - 1; int backward = length - 1;
@ -19,7 +20,7 @@ public class Palindrome {
public boolean isPalindromeReverseTheString(String text) { public boolean isPalindromeReverseTheString(String text) {
String reverse = ""; String reverse = "";
text = text.toLowerCase(); text = text.replaceAll("\\s+", "").toLowerCase();
char[] plain = text.toCharArray(); char[] plain = text.toCharArray();
for (int i = plain.length - 1; i >= 0; i--) for (int i = plain.length - 1; i >= 0; i--)
reverse += plain[i]; reverse += plain[i];
@ -37,4 +38,16 @@ public class Palindrome {
StringBuffer reverse = plain.reverse(); StringBuffer reverse = plain.reverse();
return reverse.equals(plain); 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;
}
} }

View File

@ -1,5 +1,7 @@
package com.baeldung.string; package com.baeldung.string;
import java.util.Arrays;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -68,6 +70,45 @@ public class WhenCheckingPalindrome {
@Test @Test
public void whenStringBufferSentence_shouldBePalindrome() { public void whenStringBufferSentence_shouldBePalindrome() {
for(String sentence:sentences) for(String sentence:sentences)
Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence)); 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);
}
} }