Python solution: Beat 96%, run time 76ms

  • 0

    class Solution(object):
    def isPalindrome(self, s):

        # extract alphanumeric characters
        alnum_str = ''.join([i for i in s if i.isalnum()])
        # decide if it is a palindrome
        if alnum_str.lower() == alnum_str[::-1].lower() or alnum_str == []:
            return True
            return False


  • 0

    This solution is obviously correct but not optimal, having two sentinel variables for the left and right bounds is definitely better.
    The time complexities will be the same but yours should have slower constants.

    The sentinel method stops as soon as it detects an invalid palindrome.

Log in to reply

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