Python (with comments) takes extra space. any better solution?


  • 0
    P
    class Solution(object):
        def isPalindrome(self, s):
            """
            :type s: str
            :rtype: bool
            """
            if len(s)==0:
                return True
            #CLEAN THE STRING
            #format string
            #if upper case, change to lower case
            #if not-alpha, change to ""
            s=s.replace(" ","")
            word=list(s)
            i=0
            while i<len(word):
                if word[i].isupper():
                    word[i]=word[i].swapcase()
                elif word[i].isalpha()==False and word[i].isnumeric()==False:
                    word.pop(i)
                    #word.replace(s[i],"")
                    i-=1
                i+=1
            #CHECK
            ptr1=0
            ptr2=len(word)-1
            while ptr1<ptr2:
                if word[ptr1]!=word[ptr2]:
                    return False
                ptr1+=1
                ptr2-=1
            return True

Log in to reply
 

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