알고리즘/LeetCode

125. Valid Palindrome ( 유효한 팰린드롬 )

bright_code 2020. 9. 30. 21:36
728x90
반응형

팰린드롬 : 앞 뒤가 똑같은 문자열. 

주어진 문자열이 팰린드롬인지 확인할 것. 대소문자 구분 x 영문자와 숫자만을 취급. 

 

ex ) 

INPUT = "A man, a plan, a canal: Panama"  

OUTPUT = True

 

INPUT = "0P"
OUTPUT = False 

class Solution:
    def isPalindrome(self, s: str) -> bool:
        real_s = []
        for i in s : 
            if 48<=ord(i)<=57 or 65<= ord(i) <=90 or 97<=ord(i)<=122 :
                real_s.append(i)
        
        for i in range( len(real_s)//2 ):
            if real_s[i].upper() != real_s[ len(real_s)-i-1 ].upper():
                return False 
        return True

 

tip!  

real_s.append(i)  할 때, real_s.append( i.upper() ) 로 리스트에 추가하면 더 효율적으로 코드를 작성할 수 있다. 

 

 

leetcode.com/problems/valid-palindrome/

 

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

 

 

728x90
반응형