일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- heap
- 생성형AI
- GenerativeAI
- 알고리즘
- SQL
- 투포인터
- two-pointer
- Python3
- Python
- 니트코드
- Stack
- 파이썬
- 파이썬기초100제
- 파이썬알고리즘
- 코드업
- GenAI
- 릿코드
- array
- sql코테
- gcp
- nlp
- 리트코드
- Greedy
- LeetCode
- 슬라이딩윈도우
- stratascratch
- codeup
- dfs
- 자연어처리
- slidingwindow
Archives
- Today
- Total
Tech for good
[Neetcode/String Preprocessing ] Valid Palindrome 본문
https://neetcode.io/problems/is-palindrome?list=neetcode150
NeetCode
neetcode.io
class Solution:
def isPalindrome(self, s: str) -> bool:
ss = ''
for c in s:
if c.isalnum():
ss+= c.lower()
return ss==ss[::-1]
ss = ''
- Initialize an empty string to store the cleaned version of s.
if c.isalnum():
- isalnum() checks if the character is alphanumeric (letter or number).
- This filters out spaces, punctuation, symbols, etc.
ss += c.lower()
- Convert the character to lowercase and append it to the cleaned string.
- This makes the check case-insensitive.
return ss == ss[::-1]
- ss[::-1] reverses the cleaned string.
- If the cleaned string is the same as its reverse, it's a palindrome → return True.
✅ Example Test:
s = "Was it a car or a cat I saw?"
ss = "wasitacaroracatisaw"
ss[::-1] = "wasitacaroracatisaw"
→ returns True
📌 Summary:
You implemented the correct logic using:
- isalnum() for filtering
- lower() for case insensitivity
- [::-1] for palindrome check
'IT > Computer Science' 카테고리의 다른 글
[Neetcode/Stack] Minimum Stack (0) | 2025.06.28 |
---|---|
[Leetcode/DFS, Graph] 1971. Find if Path Exists in Graph (0) | 2025.06.25 |
[Neetcode/Greedy, Hash Map, Sort] Top K Frequent Elements (0) | 2025.06.24 |
[Neetcode/Brute-force, Hashmap] Two Sum (0) | 2025.06.24 |
[Leetcode/Stack] 1047. Remove All Adjacent Duplicates In String (0) | 2025.06.11 |