Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준 9012번
- DFS & BFS
- 큐(Queue)
- 위상 정렬(Topology Sort)
- 백준 21606번
- 플로이드 워셜 알고리즘(Floyd-Warshall Algorithm)
- 백준 10000번
- 다익스트라 알고리즘(Dijkstra Algorithm)
- 백준 2812번
- 그리디 알고리즘(Greedy Algorithm)
- 동적 프로그래밍(Dynamic Programming)
- BFS(Breadth First Search)
- 백준 1948번
- 트리(Tree)
- 백준 17608번
- 백준 2261번
- BFS
- 백준 1707번
- 분할 정복(Divide and Conquer)
- 백준 2493번
- 이분 그래프(Bipartite Graph)
- 백준 2504번
- 이분 탐색(Binary Search)
- DFS
- DFS(Depth First Search)
- 알고리즘 개념
- 백준 18352번
- 스택(Stack)
- 위상 정렬(Topological Sort)
- 그래프(Graph)
Archives
- Today
- Total
Always Be Wise
문자열 조작 : 유효한 팰린드롬(리트코드 125번) 본문
728x90
▶ 문제 : https://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
##### 문제 #####
주어진 문자열이 팰린드롬인지 확인하라. 대소문자를 구분하지 않으며, 영문자와 숫자만을 대상으로 한다.
##### 입력 #####
"A man, a plan, a canal, : Panama"
##### 출력 #####
True
▶ 접근 방법
팰린드롬은 앞뒤가 똑같은 단어나 문장을 의미한다.
리스트로 변환하여 맨앞과 맨뒤를 하나 씩 비교하는 방법, 슬라이싱을 방법 등 다양한 풀이가 가능했다.
▶ 풀이 코드
ex_string = "A man, a plan, a canal, : Panama"
########################################################
def is_palindrome_using_list(string: str) -> bool:
strs = []
for char in string:
if char.isalnum():
strs.append(char.lower())
while len(strs) > 1:
if strs.pop(0) != strs.pop():
return False
return True
print(is_palindrome_using_list(ex_string))
########################################################
from collections import deque
def is_palindrome_using_deque(string: str) -> bool:
strs = deque([])
for char in string:
if char.isalnum():
strs.append(char.lower())
while len(strs) > 1:
if strs.popleft() != strs.pop():
return False
return True
print(is_palindrome_using_deque(ex_string))
########################################################
import re
def is_palindrome_using_slicing(string: str) -> bool:
string = string.lower()
string = re.sub('[^a-z0-9]', '', string)
return string == string[::-1]
print(is_palindrome_using_slicing(ex_string))
▶ 관련 링크
'알고리즘 > 리트코드' 카테고리의 다른 글
문자열 조작 : 가장 긴 팰린드롬 부분 문자열(리트코드 5번) (0) | 2021.12.14 |
---|---|
문자열 조작 : 그룹 애너그램(리트코드 49번) (0) | 2021.12.14 |
문자열 조작 : 가장 흔한 단어(리트코드 819번) (0) | 2021.12.14 |
문자열 조작 : 로그 파일 재정렬(리트코드 937번) (0) | 2021.12.14 |
문자열 조작 : 문자열 뒤집기(리트코드 344번) (0) | 2021.12.14 |
Comments