Challenge Statement

  • A phrase is a palindrome if it reads the same forward and backward after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters. Alphanumeric characters include letters and numbers.
  • Given a string s, return True if it is a palindrome or False otherwise.
  • This challenge corresponds to LeetCode #125.

Constraints

  • 1 <= s.length <= 2 * 105
  • s consists only of printable ASCII characters.

Example 1:

Input: s = "A man, a plan, a canal: Panama"

Output: True

Explanation: “amanaplanacanalpanama” is a palindrome.

Example 2:

Input: s = "race a car"

Output: False

Explanation: “raceacar” is not a palindrome.

Example 3:

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.

Solution

Below is my solution and some test cases. This solution has a linear time complexity O(n) and a constant space complexity O(1), where n is the length of the input string.