Which of these three methods is the most efficient?
Also, what is the time & space complexity of each?
public class Palindrome {
public static boolean isStringBased(String word) {
if (word == null || "".equals(word)) {
return false;
}
String lowerCasedWord = word.toLowerCase();
int i = 0;
int j = lowerCasedWord.length() - 1;
while (i < j) {
if (lowerCasedWord.charAt(i) != lowerCasedWord.charAt(j))
return false;
i++;
j--;
}
return true;
}
public static boolean isPalindromeRecursive(String input) {
if(input.length() == 0 || input.length() == 1) {
return true;
}
if(input.charAt(0) == input.charAt(input.length()-1)) {
return isPalindromeRecursive(input.substring(1, input.length()-1));
}
return false;
}
public static boolean isPalindromeIterative(String input) {
boolean retValue = false;
int length = input.length();
for( int i = 0; i < length/2; i++ ) {
if (input.charAt(i) == input.charAt(length-i-1)) {
retValue = true;
}
}
return retValue;
}
}
isPalindromeIterative("override")
: true. \$\endgroup\$