[Leetcode]125. Valid Palindrome

2024. 7. 31. 22:29Algorithm

1. problem : 

https://leetcode.com/problems/valid-palindrome/

 

2. solution 1 :

class Solution:
    def isPalindrome(self, s: str) -> bool:
        new = ''
        for a in s:
            if a.isalpha() or a.isdigit():
                new += a.lower()
        return (new == new[::-1])

new와 new를 reverse 시킨 것을 비교해서 return 한다. 

 

3. solution 2 :

class Solution:
    def isPalindrome(self, s: str) -> bool:
        s = ''.join([char for char in s if char.isalnum() ]).lower()
        left,right = 0 , len(s)-1 
        while left < right:
            if s[left] == s[right]:
                left += 1 
                right -= 1 
            else:
                return False 
        return True

two pointer 기법으로 양측에서 좁혀가며 비교한다.