BAEL-1525: Review comments incorporated. (#3639)
This commit is contained in:
parent
a0c907ba7c
commit
4d64a12271
|
@ -5,49 +5,54 @@ import java.util.stream.IntStream;
|
||||||
public class Palindrome {
|
public class Palindrome {
|
||||||
|
|
||||||
public boolean isPalindrome(String text) {
|
public boolean isPalindrome(String text) {
|
||||||
text = text.replaceAll("\\s+", "")
|
String clean = text.replaceAll("\\s+", "").toLowerCase();
|
||||||
.toLowerCase();
|
int length = clean.length();
|
||||||
int length = text.length();
|
|
||||||
int forward = 0;
|
int forward = 0;
|
||||||
int backward = length - 1;
|
int backward = length - 1;
|
||||||
boolean palindrome = true;
|
|
||||||
while (backward > forward) {
|
while (backward > forward) {
|
||||||
char forwardChar = text.charAt(forward++);
|
char forwardChar = clean.charAt(forward++);
|
||||||
char backwardChar = text.charAt(backward--);
|
char backwardChar = clean.charAt(backward--);
|
||||||
if (forwardChar != backwardChar)
|
if (forwardChar != backwardChar)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return palindrome;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPalindromeReverseTheString(String text) {
|
public boolean isPalindromeReverseTheString(String text) {
|
||||||
String reverse = "";
|
StringBuilder reverse = new StringBuilder();
|
||||||
text = text.replaceAll("\\s+", "").toLowerCase();
|
String clean = text.replaceAll("\\s+", "").toLowerCase();
|
||||||
char[] plain = text.toCharArray();
|
char[] plain = clean.toCharArray();
|
||||||
for (int i = plain.length - 1; i >= 0; i--)
|
for (int i = plain.length - 1; i >= 0; i--)
|
||||||
reverse += plain[i];
|
reverse.append(plain[i]);
|
||||||
return reverse.equals(text);
|
return (reverse.toString()).equals(clean);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPalindromeUsingStringBuilder(String text) {
|
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();
|
StringBuilder reverse = plain.reverse();
|
||||||
return reverse.equals(plain);
|
return (reverse.toString()).equals(clean);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPalindromeUsingStringBuffer(String text) {
|
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();
|
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)
|
if (forward == backward)
|
||||||
return true;
|
return true;
|
||||||
if ((text.charAt(forward)) != (text.charAt(backward)))
|
if ((text.charAt(forward)) != (text.charAt(backward)))
|
||||||
return false;
|
return false;
|
||||||
if (forward < backward + 1) {
|
if (forward < backward + 1) {
|
||||||
return isPalindromeRecursive(text, forward + 1, backward - 1);
|
return recursivePalindrome(text, forward + 1, backward - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -58,5 +63,4 @@ public class Palindrome {
|
||||||
return IntStream.range(0, temp.length() / 2)
|
return IntStream.range(0, temp.length() / 2)
|
||||||
.noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1));
|
.noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,84 +25,73 @@ public class PalindromeTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenWord_shouldBePalindrome() {
|
public void whenWord_shouldBePalindrome() {
|
||||||
for(String word:words)
|
for (String word : words)
|
||||||
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)
|
||||||
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)
|
||||||
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)
|
||||||
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)
|
||||||
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)
|
||||||
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)
|
||||||
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)
|
||||||
|
|
||||||
assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence));
|
assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPalindromeRecursive_wordShouldBePalindrome() {
|
public void whenPalindromeRecursive_wordShouldBePalindrome() {
|
||||||
for(String word:words) {
|
for (String word : words)
|
||||||
word = word.replaceAll("\\s+", "").toLowerCase();
|
assertTrue(palindrome.isPalindromeRecursive(word));
|
||||||
int backward = word.length()-1;
|
|
||||||
|
|
||||||
assertTrue(palindrome.isPalindromeRecursive(word,0,backward));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPalindromeRecursive_sentenceShouldBePalindrome() {
|
public void whenPalindromeRecursive_sentenceShouldBePalindrome() {
|
||||||
for(String sentence:sentences) {
|
for (String sentence : sentences)
|
||||||
sentence = sentence.replaceAll("\\s+", "").toLowerCase();
|
assertTrue(palindrome.isPalindromeRecursive(sentence));
|
||||||
int backward = sentence.length()-1;
|
|
||||||
|
|
||||||
assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPalindromeStreams_wordShouldBePalindrome() {
|
public void whenPalindromeStreams_wordShouldBePalindrome() {
|
||||||
for(String word:words) {
|
for (String word : words)
|
||||||
assertTrue(palindrome.isPalindromeUsingIntStream(word));
|
assertTrue(palindrome.isPalindromeUsingIntStream(word));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPalindromeStreams_sentenceShouldBePalindrome() {
|
public void whenPalindromeStreams_sentenceShouldBePalindrome() {
|
||||||
for(String sentence:sentences) {
|
for (String sentence : sentences)
|
||||||
assertTrue(palindrome.isPalindromeUsingIntStream(sentence));
|
assertTrue(palindrome.isPalindromeUsingIntStream(sentence));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue