Online Judge/LeetCode
[LeetCode][Python] 125. Valid Palindrome
vince joe
2024. 9. 8. 01:09
- 문제 링크: https://leetcode.com/problems/valid-palindrome/
- 난이도: Easy
주어진 문자열에서 알파벳, 숫자를 제외한 문자열이 팰린드롬(앞에서 읽으나 뒤에서 읽으나 같음)인지 반환하는 문제다
추가로 대소문자도 무시한다
투포인터로 푼다.
파이썬 문자열의 isalnum() 메소드로 알파벳 + 숫자 여부 체크할 수 있고, lower()로 소문자 만들 수 있다.
class Solution:
def isPalindrome(self, s: str) -> bool:
start, end = 0, len(s)-1
while start < end:
c1 = s[start]
c2 = s[end]
if not c1.isalnum():
start += 1
continue
if not c2.isalnum():
end -= 1
continue
if c1.lower() != c2.lower():
return False
start += 1
end -= 1
return True
반응형