Created
April 6, 2024 10:36
-
-
Save mmasias/beaea4b5a1429b4811dce0dc229fda60 to your computer and use it in GitHub Desktop.
Detección de una cadena de texto palindrómica #recursividad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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