Python. Simple and super fast solution.


  • 0
    P
    class Solution(object):                                                                                                                                                                                                               
        def isPalindrome(self, s):
            if len(s) <= 1:
                return True                                                                                                                                          
            left = 0
            right = len(s) - 1
            isPali = True                                                                                                                                                                                                                 
            while isPali and left < right:
                if not s[left].isalnum():
                    left += 1
                    continue                                                                                                                                                                                                              
                if not s[right].isalnum():
                    right -= 1
                    continue                                                                                                                                                                                                              
                if s[left].lower() != s[right].lower():
                    isPali = False                                                                                                                                                                                                        
                left += 1
                right -= 1                                                                                                                                           
            return isPali
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.