Valid Palindrome Leetcode Solution Python

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.

Leetcode Solution (Python)

class Solution:
    def isPalindrome(self, s: str) -> bool:
        # To lower case
        s = s.lower()
        
        # Remove puncutation
        s = s.translate(str.maketrans('', '', string.punctuation))
        
        # Remove space
        s = s.replace(" ", "")
        
        # Two pointers
        l = 0
        r = len(s)-1
        while l<r:
            if s[l]!=s[r]:
                return False
            l+=1
            r-=1
        
        return True

Elements of Programming Interviews in Python: The Insiders’ Guide

Unknown's avatar

Author: mathtuition88

Math and Education Blog

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.