새오의 개발 기록

Leetcode 125: 유효한 팰린드롬 본문

Algorithm/Leetcode

Leetcode 125: 유효한 팰린드롬

새오: 2022. 9. 30. 20:17

 

주어진 문자열이 팰린드롬인지 확인하라.
대소문자를 구분하지 않으며, 영문자와 숫자만을 대상으로 한다.

* 팰린드롬이란
앞뒤가 똑같은 단어나 문장으로, 뒤집어도 같은 말이 되는 단어 또는 문장

ex) '소주 만 병만 주소'


 

Valid Palindrome - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

// 입력 예제
["A man, a plan, a canal: Panama"]

// 출력 예제
true

 

 

 

풀이

 

1. 리스트로 변환

 

def isPalindrome(self, s):

    strs = []

    for c in s:
        if c.isalnum():
            strs.append(c.lower())

    # 팰린드롬 여부 판별
    while len(strs) > 1:
        # 맨 앞과 맨 뒤를 비교
        if strs.pop(0) != strs.pop():
            return False
    return True
    
    
    
# isalpha() 문자열이 영어 혹은 한글로 되어있으면 참 리턴, 아니면 거짓 리턴.
# isalnum() 문자열이 영어, 한글 혹은 숫자로 되어있으면 참 리턴, 아니면 거짓 리턴.

 

 

 

 

'Algorithm > Leetcode' 카테고리의 다른 글

Leetcode 1: 두 수의 합  (0) 2022.10.03
Leetcode 49: 그룹 애너그램  (0) 2022.10.02
Leetcode 819: 가장 흔한 단어  (0) 2022.10.01
Leetcode 937: 로그 파일 재정렬  (0) 2022.10.01
Leetcode 344: 문자열 뒤집기  (1) 2022.09.30