| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- Algorithm
- 2022
- 프로그래머스
- LEVEL 2
- hackerrank
- K8S
- Python
- 파이썬
- 알고리즘
- C++
- MySQL
- Count Monobit Integers
- 하늘과 바람과 별과 시
- leetcode
- 리트코드
- Code Jam
- 하늘과 바람과 별과 詩
- 3D PRINTING
- swift
- 코딩테스트
- Code Jam 2022
- 문제해결
- ProblemSolving
- Kubernetes
- Qualification Round
- 해커랭크
- First Unique Character in a String
- Certbot/dns-route53
- GitLab
Archives
- Today
- Total
공대생의 비망록
[LeetCode][Easy] Valid Palindrome 문제 Python 풀이 본문
Programming Language/Python
[LeetCode][Easy] Valid Palindrome 문제 Python 풀이
myungsup1250 2026. 2. 9. 10:46영어 대소문자와 공백, 특수문자가 섞여 있는 문자열에서 영어 소문자만 가지고 회문 여부를 검증하는 문제.
실제로 요구하는 대로 소문자만 존재하는 문자열로 가공하여 회문 여부를 검증하는 방법과 주어진 문자열 그대로 투 포인터를 사용하여 문제를 해결하는 방법을 각각 구현해보았습니다.
문자열 가공 후 회문을 판단하는 방법:
class Solution:
def isPalindrome(self, s: str) -> bool:
lower = s.lower()
trimmed = str()
for ch in lower:
if ch.islower() or ch.isdigit():
trimmed += ch
for i in range(len(trimmed) // 2):
if trimmed[i] != trimmed[-i - 1]:
return False
return True
주어진 문자열 그대로 투 포인터로 문제를 해결하는 방법:
class Solution:
def isPalindrome(self, s: str) -> bool:
left = 0
right = len(s) - 1 # left, right = 0, len(s) - 1
while left < right:
while left < right and not s[left].isalnum():
left += 1
while left < right and not s[right].isalnum():
right -= 1
if s[left].lower() != s[right].lower():
return False
left += 1
right -= 1
return True'Programming Language > Python' 카테고리의 다른 글
| [LeetCode][Easy] First Unique Character in a String 문제 Python 풀이 (0) | 2026.02.09 |
|---|---|
| [LeetCode][Easy] Valid Anagram 문제 Python 풀이 (0) | 2026.02.08 |
| [LeetCode][Easy] Count Monobit Integers 문제 Python 풀이 (0) | 2026.02.08 |
| [LeetCode][Easy] Single Number 문제 Python 풀이 (0) | 2026.02.08 |
| [LeetCode][Easy] Missing Number 문제 Python 풀이 (0) | 2026.02.08 |
Comments
