Algorithm/Online judge

[LeetCode] Strings > Valid Palindrome

민철킹 2021. 5. 25. 23:28

https://leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/883/

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com

 


풀이

Palindrome이란 뒤집어도 동일한 문자를 뜻한다. 

isdigit과 isalpha 메서드를 통해 문자와 숫자만 리스트에 담고 이 리스트와 reversed를 통해 뒤집은 리스트와 동일한지 확인한다.

 

class Solution:
    def isPalindrome(self, s: str) -> bool:
        alpha_list = []
        for i in s:
            if i.isalpha() or i.isdigit():
                alpha_list.append(i.lower())
        if alpha_list == list(reversed(alpha_list)):
            return True
        else:
            return False

 

반응형

'Algorithm > Online judge' 카테고리의 다른 글

[백준] 14719번 > 빗물  (0) 2021.05.28
[LeetCode] Strings > Longest Common Prefix  (0) 2021.05.26
[LeetCode] Strings > Valid Anagram  (0) 2021.05.25
[LeetCode] Strings > Reverse Integer  (0) 2021.05.21
[백준] 15868번 > 치킨 배달  (0) 2021.05.20