[leetcode] 125. Valid Palindrome

Joy·2022년 10월 13일
0

Coding Test

목록 보기
46/48

Q

Given a string s, return true if it is a palindrome, or false otherwise.

A


import re
class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        
        s = s.replace(" ","").lower()

        s = re.sub('[^a-z0-9]+', '', s)
        
        if (len(s)==0):return True
        else:
            for i in range(int(len(s)/2)):
                if (s[i] == s[-(i+1)]):
                    continue
                else : 
                    return False
        return True
  • regex -> subsitute non alpha or num to ''
  • string.replace -> easily replace
  • string.lower()
  • len(string) : float. must be changed to int
profile
roundy

0개의 댓글