2021. 1. 4. 13:52ㆍProblem Solving/LeetCode
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
팰린드롭 이란?
앞뒤가 똑같은 단어나 문장. 뒤집어도 같은 말이 된다. 우리말로는 '회문'이라고 한다.
내 풀이
class Solution: def isPalindrome(self, s: str) -> bool: strs = [] # 소문자로 변환 for i in s: if i.isalnum(): strs.append(i.lower()) tmp = strs # 펠린드롭 판단 while len(tmp) > 1: if strs.pop(0) != tmp.pop(): return False return True

pop(0)이 O(n), 이것을 n번 반복하므로 O(n^2)이다.
문제에서 영문자와 숫자만을 대상으로 한다고 하였으니 isalnum()을 사용했다.
isalnum()은 문자+숫자까지 True를 리턴한다.
https://developeryuseon.tistory.com/28
[Python] isalnum(), isalpha() 함수
isalnum() 문자열이 영어, 한글, 숫자로 되어있으면 참 리턴 아니면 거짓 리턴 isalpha() 문자열이 영어, 한글로 되어있으면 참 리턴 아니면 거짓 리턴 str = "!@#123가나다abc" for char in str: if char.isalnum..
developeryuseon.tistory.com
+) Deque를 사용하면 더 빠르게 구현 가능하다. pop(0) 보다 popleft()의 연산 속도가 더 빠르다
책 정보
![]() |
|
'Problem Solving > LeetCode' 카테고리의 다른 글
[LeetCode] 5. Longest Palindromic Substring (0) | 2021.01.07 |
---|---|
[LeetCode] 49. Group Anagrams (0) | 2021.01.06 |
[LeetCode] 819. Most Common Word (0) | 2021.01.05 |
[LeetCode] 937. Reorder Data in Log Files (0) | 2021.01.04 |
[LeetCode] 344. Reverse String (0) | 2021.01.04 |