BAEL-1525: New changes added.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user