< Back




125. Valid Palindrome

We're asked to return True or False if some input string, s, is a Valid Palindrome - after removing
all non-alphanumeric characters and ignoring case. Pretty simple, we use Python's filter() method
and the str.islanum property to filter out all non-alphanumeric characters. Then we convert all
characters to lowercase and check if the string is equal to its reverse.

The solution is as follows:


  class Solution:
      def isPalindrome(self, s: str) -> bool:
          s = ''.join(filter(str.isalnum, s)).lower()
          return s == s[::-1]


_ Time Complexity:

  O(n) - Where n is the size of s, we traverse the entire string.

_ Space Complexity:

  O(n) - We store the sanitized string.