Python solution, O(N) time, O(1) space (Commented)


  • 0
    N
    class Solution(object):
        def isPalindrome(self, s):
            """
            :type s: str
            :rtype: boola
            """
            # ord('0') == 48
            is_number = lambda x: x - 48 >= 0 and x - 48 <= 9
    
            # ord('a') == 97
            is_lowercase = lambda x: x - 97 >= 0 and x - 97 <= 25
    
            # ord('A') == 65
            is_uppercase = lambda x: x - 65 >= 0 and x - 65 <= 25
    
            # ord('a') - ord('A') == 32
            same_letters = lambda x, y: abs(x - y) == 32
    
            alphanumeric = lambda x: is_number(x) or is_lowercase(x)\
                    or is_uppercase(x)
    
            # Compare only alphanumeric letters
            s = [x for x in [ord(y) for y in s] if alphanumeric(x)]
    
            for i in xrange(len(s) / 2):
                left, right = s[i], s[-(1 + i)]
                if (left != right and
                        (is_number(left) or not same_letters(left, right))):
                    return False
    
            return True
    
    

Log in to reply
 

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