Skip to content

Instantly share code, notes, and snippets.

@mmasias
Created April 6, 2024 10:36
Show Gist options
  • Save mmasias/beaea4b5a1429b4811dce0dc229fda60 to your computer and use it in GitHub Desktop.
Save mmasias/beaea4b5a1429b4811dce0dc229fda60 to your computer and use it in GitHub Desktop.
Detección de una cadena de texto palindrómica #recursividad
public class PalindromeChecker {
public static boolean isPalindrome(String string) {
String strippedString = string.replaceAll("\\W", "");
int length = strippedString.length();
if (length > 1) {
return palindrome(strippedString.toLowerCase(), 0, length - 1);
}
return false;
}
private static boolean palindrome(String string, int left, int right) {
if (left >= right) {
return true;
}
char lhs = string.charAt(left);
char rhs = string.charAt(right);
if (lhs != rhs) {
return false;
}
return palindrome(string, left + 1, right - 1);
}
public static void main(String[] args) {
String testStr = "A man, a plan, a canal: Panama";
System.out.println("¿Es palíndromo? " + isPalindrome(testStr));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment