Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * Test a string to see if it is a palindrome.
- *
- * @param str
- * A string to be tested to see if it is a palindrome
- * @return true if str is a palindrome, false otherwise
- *
- *
- */
- public static boolean isPalindrome(String str) {
- if(str.length() == 0) return true;
- else if(str.length() % 2 != 0)return isPalindromeO(str, 0, str.length()-1);
- else return isPalindromeE(str, 0, str.length()-1);
- }
- /**
- * Recursive method to test if a string is a palindrome.
- *
- * @return true if str is a palindrome, false otherwise
- *
- * Add parameters however you see fit.
- */
- private static boolean isPalindromeO(String str, int a, int b) {
- if(str.charAt(a) != str.charAt(b))return false;
- if(a == b && str.charAt(a) == str.charAt(b))return true;
- else return isPalindromeO(str, ++a, --b);
- }
- /**
- * Recursive method to test if a string is a palindrome.
- *
- * @return true if str is a palindrome, false otherwise
- *
- * Add parameters however you see fit.
- */
- private static boolean isPalindromeE(String str, int a, int b) {
- if(a > b)return true;
- if(str.charAt(a) != str.charAt(b))return false;
- return isPalindromeE(str, ++a, --b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement