문제: https://neetcode.io/problems/is-palindrome
NeetCode
neetcode.io
[내 풀이]
class Solution:
def isPalindrome(self, s: str) -> bool:
s = ''.join(x for x in s if x.isalnum()).lower()
for i in range(len(s)//2):
l = 0 + i
r = len(s) - 1 - i
if s[l] == s[r]:
pass
else:
return False
return True
python은 코테 언어다....
[솔루션 풀이]
class Solution:
def isPalindrome(self, s: str) -> bool:
l, r = 0, len(s) - 1
while l < r:
while l < r and not self.alphaNum(s[l]):
l += 1
while r > l and not self.alphaNum(s[r]):
r -= 1
if s[l].lower() != s[r].lower():
return False
l, r = l + 1, r - 1
return True
def alphaNum(self, c):
return (ord('A') <= ord(c) <= ord('Z') or
ord('a') <= ord(c) <= ord('z') or
ord('0') <= ord(c) <= ord('9'))
시간 복잡도
n은 문자열의 길이
O(n/2)