Valid Palindrome

Problem

https://leetcode.com/problems/valid-palindrome/

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.

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.

Constraints:

  • 1 <= s.length <= 2 * 10:sup:`5`

  • s consists only of printable ASCII characters.

Pattern

Two Pointers, String

Approaches

Explanation

First, we convert s to all lowercase. Rather than remove all non-alphanumeric characters, we can ignore them when checking if s is a palindrome. We do this by using two pointers i and j at the beginning of s and at the end of s. When both pointers are on non-alphanumeric characters, move them inwards.

Code

def isPalindrome(s: str) -> bool:
    """Check if ``s`` is a palindrome, ignoring non-alphanumeric characters."""
    s = s.lower()
    s = "".join(filter(str.isalnum, s))

    left = 0
    right = len(s) - 1
    while left < right:
        if s[left] != s[right]:
            return False
        else:
            left += 1
            right -= 1
    return True

Test

>>> from valid_palindrome__two_pointers import isPalindrome
>>> isPalindrome('A man, a plan, a canal: Panama')
True
>>> isPalindrome('race a car')
False
>>> isPalindrome(' ')
True
>>> isPalindrome('0P')
False

Complexity

\(n\) is the length of s.

Measure

Complexity

Notes

Time

\(O(n)\)

single pass through s.

Auxiliary Space

\(O(n)\)

lowercase copy

valid_palindrome__two_pointers.isPalindrome(s: str) bool

Check if s is a palindrome, ignoring non-alphanumeric characters.