For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring()
is an expensive operation, especially since Java 7 especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.
Single return statements are overrated. There's no practical advantage to forcing your code to have just one return
for its own sake.
Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.
You take the string length often enough that it's worth assigning it to a variable.
Overall, the function could be written more compactly.
public static boolean isPalindrome(String s) {
int len = s.length();
if (len <= 1) {
return true;
}
return (s.charAt(0) == s.charAt(len - 1)) &&
isPalindrome(s.substring(1, len - 1));
}
For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring()
is an expensive operation, especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.
Single return statements are overrated. There's no practical advantage to forcing your code to have just one return
for its own sake.
Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.
You take the string length often enough that it's worth assigning it to a variable.
Overall, the function could be written more compactly.
public static boolean isPalindrome(String s) {
int len = s.length();
if (len <= 1) {
return true;
}
return (s.charAt(0) == s.charAt(len - 1)) &&
isPalindrome(s.substring(1, len - 1));
}
For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring()
is an expensive operation, especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.
Single return statements are overrated. There's no practical advantage to forcing your code to have just one return
for its own sake.
Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.
You take the string length often enough that it's worth assigning it to a variable.
Overall, the function could be written more compactly.
public static boolean isPalindrome(String s) {
int len = s.length();
if (len <= 1) {
return true;
}
return (s.charAt(0) == s.charAt(len - 1)) &&
isPalindrome(s.substring(1, len - 1));
}
For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring()
is an expensive operation, especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.
Single return statements are overrated. There's no practical advantage to forcing your code to have just one return
for its own sake.
Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.
You take the string length often enough that it's worth assigning it to a variable.
Overall, the function could be written more compactly.
public static boolean isPalindrome(String s) {
int len = s.length();
if (len <= 1) {
return true;
}
return (s.charAt(0) == s.charAt(len - 1)) &&
isPalindrome(s.substring(1, len - 1));
}
For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring()
is an expensive operation, especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.
Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.
You take the string length often enough that it's worth assigning it to a variable.
Overall, the function could be written more compactly.
public static boolean isPalindrome(String s) {
int len = s.length();
if (len <= 1) {
return true;
}
return (s.charAt(0) == s.charAt(len - 1)) &&
isPalindrome(s.substring(1, len - 1));
}
For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring()
is an expensive operation, especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.
Single return statements are overrated. There's no practical advantage to forcing your code to have just one return
for its own sake.
Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.
You take the string length often enough that it's worth assigning it to a variable.
Overall, the function could be written more compactly.
public static boolean isPalindrome(String s) {
int len = s.length();
if (len <= 1) {
return true;
}
return (s.charAt(0) == s.charAt(len - 1)) &&
isPalindrome(s.substring(1, len - 1));
}
For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring()
is an expensive operation, especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.
Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.
You take the string length often enough that it's worth assigning it to a variable.
Overall, the function could be written more compactly.
public static boolean isPalindrome(String s) {
int len = s.length();
if (len <= 1) {
return true;
}
return (s.charAt(0) == s.charAt(len - 1)) &&
isPalindrome(s.substring(1, len - 1));
}