// Efficient : Time Complexity : O(n), Space Complexity : Θ(1)
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int begin = 0, end = str.length() - 1;
// While there are characters to compare
while (begin < end) {
// If there is a mismatch
if (str.charAt(begin) != str.charAt(end))
return false;
// Increment first pointer and
// decrement the other
begin++;
end--;
}
// Given string is a palindrome
return true;
}
// Naive : Time Complexity : Θ(n), Space Complexity : Θ(n)
static boolean isPalindrome(String str)
{
StringBuilder rev = new StringBuilder(str);
rev.reverse(); // StringBuilder is mutable & has a function called reverse
return str.equals(rev.toString());
}