문제 링크 >> https://leetcode.com/problems/valid-palindrome/
📋 문제
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
👉 입출력
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
Example 3:
Input: s = " "
Output: true
Explanation: s is an empty string "" after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.
💡 사용된 개념
✔️ str.isalnum()
문자열이 알파벳([a-zA-Z])과 숫자([0-9])로만 구성되었는지 확인하는 파이썬의 문자열 메서드이다.
문자열이 모두 알파벳과 숫자로만 구성되었다면 True, 아니면 False를 반환한다.
📝 풀이
class Solution:
def isPalindrome(self, s: str) -> bool:
s = ''.join(filter(str.isalnum, s)).lower()
return True if s==s[::-1] else False
🔍 참조
str.isalnum() http://www.w3big.com/ko/python/att-string-isalnum.html
'Algorithm > Python' 카테고리의 다른 글
[LeetCode] Container With Most Water (0) | 2022.04.11 |
---|---|
[LeetCode] Assign Cookies (0) | 2022.04.09 |
[LeetCode] Palindrome Number (0) | 2022.04.09 |
[백준 10825번] 국영수 (0) | 2022.04.06 |
[백준 10814번] 나이순 정렬 (0) | 2022.04.06 |
댓글