Leetcode 125 "Valid Palindrome"

125. Valid Palindrome

Result

Runtime: 11ms Beats 36.15% Memory: 18.16MB Beats 58.11%

class Solution:
    def isPalindrome(self, s: str) -> bool:
        """
        set left as 0
        right as len(s)-1

        repeat until left is less then right:
        until character at position left is a letter left = left + 1 and lower then length of s
        until character at position right is a letter right = right - 1 and higher then zero
        if character at left is different then character at right return false
        end repeat

        if a the end of the cycle we didnt return false, return true
        """
        left = 0
        right = len(s) - 1
        while left < right:
            while not s[left].isalnum() and left < len(s)-1:
                left = left + 1
            while not s[right].isalnum() and left < right:
                right = right - 1
            if s[left].upper() != s[right].upper():
                return False
                
            left = left + 1
            right = right - 1
        return True
        """
        ONLY WORKS IF INPUT IS ONLY LETTERS
        amanaplana c analpanama: is palindrome
        amanaplana cc analpanama: is palindrome
        if string s length is pair:
            left = len(s) - 1
            right = len(s)
        else
            left = len(s) + 1
            right = len(s) + 1
        repeat until left is 0 and right is same as string s length
            if s(left) != s(right):
                return false
            else:
                return true
        """       
        # if len(s) % 2 == 0:
        #     left = int((len(s)/2) - 1)
        #     right = int(len(s)/2)
        # else:
        #     left = int((len(s)/2) + 1)
        #     right = int((len(s)/2) + 1)
        # while left != 0 and right != len(s)-1:
        #     if s[left] != s[right]:
        #         return False
        #     left = left + 1
        #     right = right - 1
        # return True
    Clicky