commit
f7e41c7a17
|
@ -1,5 +1,7 @@
|
||||||
package com.baeldung.string;
|
package com.baeldung.string;
|
||||||
|
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
public class Palindrome {
|
public class Palindrome {
|
||||||
|
|
||||||
public boolean isPalindrome(String text) {
|
public boolean isPalindrome(String text) {
|
||||||
|
@ -50,4 +52,11 @@ public class Palindrome {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isPalindromeUsingIntStream(String text) {
|
||||||
|
String temp = text.replaceAll("\\s+", "").toLowerCase();
|
||||||
|
return IntStream.range(0, temp.length() / 2)
|
||||||
|
.noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
package com.baeldung.string;
|
package com.baeldung.string;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class WhenCheckingPalindrome {
|
public class PalindromeTest {
|
||||||
|
|
||||||
private String[] words = {
|
private String[] words = {
|
||||||
"Anna",
|
"Anna",
|
||||||
|
@ -28,50 +26,50 @@ public class WhenCheckingPalindrome {
|
||||||
@Test
|
@Test
|
||||||
public void whenWord_shouldBePalindrome() {
|
public void whenWord_shouldBePalindrome() {
|
||||||
for(String word:words)
|
for(String word:words)
|
||||||
Assert.assertTrue(palindrome.isPalindrome(word));
|
assertTrue(palindrome.isPalindrome(word));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSentence_shouldBePalindrome() {
|
public void whenSentence_shouldBePalindrome() {
|
||||||
for(String sentence:sentences)
|
for(String sentence:sentences)
|
||||||
Assert.assertTrue(palindrome.isPalindrome(sentence));
|
assertTrue(palindrome.isPalindrome(sentence));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenReverseWord_shouldBePalindrome() {
|
public void whenReverseWord_shouldBePalindrome() {
|
||||||
for(String word:words)
|
for(String word:words)
|
||||||
Assert.assertTrue(palindrome.isPalindromeReverseTheString(word));
|
assertTrue(palindrome.isPalindromeReverseTheString(word));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenReverseSentence_shouldBePalindrome() {
|
public void whenReverseSentence_shouldBePalindrome() {
|
||||||
for(String sentence:sentences)
|
for(String sentence:sentences)
|
||||||
Assert.assertTrue(palindrome.isPalindromeReverseTheString(sentence));
|
assertTrue(palindrome.isPalindromeReverseTheString(sentence));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenStringBuilderWord_shouldBePalindrome() {
|
public void whenStringBuilderWord_shouldBePalindrome() {
|
||||||
for(String word:words)
|
for(String word:words)
|
||||||
Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(word));
|
assertTrue(palindrome.isPalindromeUsingStringBuilder(word));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenStringBuilderSentence_shouldBePalindrome() {
|
public void whenStringBuilderSentence_shouldBePalindrome() {
|
||||||
for(String sentence:sentences)
|
for(String sentence:sentences)
|
||||||
Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence));
|
assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenStringBufferWord_shouldBePalindrome() {
|
public void whenStringBufferWord_shouldBePalindrome() {
|
||||||
for(String word:words)
|
for(String word:words)
|
||||||
Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(word));
|
assertTrue(palindrome.isPalindromeUsingStringBuffer(word));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenStringBufferSentence_shouldBePalindrome() {
|
public void whenStringBufferSentence_shouldBePalindrome() {
|
||||||
for(String sentence:sentences)
|
for(String sentence:sentences)
|
||||||
|
|
||||||
Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence));
|
assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -80,7 +78,7 @@ public class WhenCheckingPalindrome {
|
||||||
word = word.replaceAll("\\s+", "").toLowerCase();
|
word = word.replaceAll("\\s+", "").toLowerCase();
|
||||||
int backward = word.length()-1;
|
int backward = word.length()-1;
|
||||||
|
|
||||||
Assert.assertTrue(palindrome.isPalindromeRecursive(word,0,backward));
|
assertTrue(palindrome.isPalindromeRecursive(word,0,backward));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,25 +88,21 @@ public class WhenCheckingPalindrome {
|
||||||
sentence = sentence.replaceAll("\\s+", "").toLowerCase();
|
sentence = sentence.replaceAll("\\s+", "").toLowerCase();
|
||||||
int backward = sentence.length()-1;
|
int backward = sentence.length()-1;
|
||||||
|
|
||||||
Assert.assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward));
|
assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPalindromeStreams_wordShouldBePalindrome() {
|
public void whenPalindromeStreams_wordShouldBePalindrome() {
|
||||||
String[] expected = Arrays.stream(words)
|
for(String word:words) {
|
||||||
.filter(palindrome::isPalindrome)
|
assertTrue(palindrome.isPalindromeUsingIntStream(word));
|
||||||
.toArray(String[]::new);
|
}
|
||||||
|
|
||||||
Assert.assertArrayEquals(expected, words);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPalindromeStreams_sentenceShouldBePalindrome() {
|
public void whenPalindromeStreams_sentenceShouldBePalindrome() {
|
||||||
String[] expected = Arrays.stream(sentences)
|
for(String sentence:sentences) {
|
||||||
.filter(palindrome::isPalindrome)
|
assertTrue(palindrome.isPalindromeUsingIntStream(sentence));
|
||||||
.toArray(String[]::new);
|
}
|
||||||
|
|
||||||
Assert.assertArrayEquals(expected, sentences);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue