Valid Palindrome - Solution
Solutions and explanations

Video Explanation

class Solution:
    def isPalindrome(self, s: str) -> bool:
        filtered = [char.lower() for char in s if char.isalnum()]
        return filtered == filtered[::-1]

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Here, n is the length of the input string.

class Solution:
    def isPalindrome(self, s: str) -> bool:
        left, right = 0, len(s) - 1
        while left < right:
            if not s[left].isalnum():
                left += 1
            elif not s[right].isalnum():
                right -= 1
            elif s[left].lower() != s[right].lower():
                return False
            else:
                left += 1
                right -= 1
        return True

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Here, n is the length of the input string.