A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false otherwise.

Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
Input: s = " "
Output: true
Explanation: s is an empty string "" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome.
Constraints:

Contents

In this approach, we will use two pointers start to point beginning of the string and end to point at the end of string, and check whether the string s has palindrome properties or not.

Implementation steps:
public class ValidPalindrome { static boolean isPalindrome(String s) { int start = 0; int end = s.length() -1; while(start < end) { char left = s.charAt(start); char right = s.charAt(end); if(!Character.isLetterOrDigit(left)) { start++; } else if (!Character.isLetterOrDigit(right)){ end--; } else if (Character.toLowerCase(left) != Character.toLowerCase(right)) { return false; } else { start++; end--; } } return true; } public static void main(String[] args) { System.out.println(isPalindrome("A man, a plan, a canal: Panama")); System.out.println(isPalindrome("race a car")); System.out.println(isPalindrome(" ")); } }
Complexity Analysis:

Time complexity: Above code runs in O(n) time where n is the input string length.
Space complexity: O(1).

Above implementations source code can be found at GitHub link for Java code