[Leetcode]125. Valid Palindrome
2024. 7. 31. 22:29ㆍAlgorithm
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 기법으로 양측에서 좁혀가며 비교한다.
'Algorithm' 카테고리의 다른 글
[Leetcode]42. Trapping Rain Water(x) (0) | 2024.08.01 |
---|---|
[Leetcode]167. Two Sum II - Input Array Is Sorted (0) | 2024.07.31 |
[Leetcode]128. Longest Consecutive Sequence (0) | 2024.07.31 |
[Leetcode]36. Valid Sudoku(x) (0) | 2024.07.31 |
[Leetcode]1456. Maximum Number of Vowels in a Substring of Given Length (0) | 2024.07.30 |