- Reference
- ๋ฌธ์ ๋ฅผ ์ ๋๋ก ์ ์ฝ๊ณ
isdigit()
๋ก ํด์ ์ค๋ ๊ฑธ๋ฆผ
- ๋, ํฌํฌ์ธํฐ ์ ์ฐ๊ณ ๋ฌธ์์ด๋ก ํ๋ค๊ฐ ์ ๋ผ์ ๊ทธ๋ฅ ๋์๊ฐ
- ํฌํฌ์ธํฐ๋ก ํ ๋๋ง๋ค ์ง๋ฆฟํ๋ค..
Solution
class Solution:
def isPalindrome(self, s: str) -> bool:
# 1. ์ํ๋ฒณ ์ ์ธํ ๋ชจ๋ ๋ฌธ์ ์ ๊ฑฐ
for w in s:
# ์ํ๋ฒณ์ด ์๋๋ผ๋ฉด ๊ทธ ๋ฌธ์๋ ๋ค ์์ ์ฃผ๊ธฐ
if not w.isalpha() and not w.isdigit():
s = s.replace(w, '')
# 2. lowerCase
s = s.lower()
# 3. ๋ค์ง์ด๋ ๊ฐ์์ง ๋น๊ต (์คํจ)
# 0P๋ False, a๋ True
# if len(s) == 2:
# if s[0] == s[1]:
# return True
# else:
# return False
#
# if len(s) % 2 == 0:
# if s[:len(s) // 2 + 1] == s[len(s) // 2 + 1:][::-1]:
# return True
# else:
# return False
# else:
# if s[:len(s) // 2] == s[len(s) // 2 + 1:][::-1]:
# return True
# else:
# return False
# 3. ๋ค์ง์ด๋ ๊ฐ์์ง ๋น๊ต
lt, rt = 0, len(s) - 1
if len(s) % 2 == 0:
for _ in range(len(s) // 2):
if s[lt] != s[rt]:
return False
lt += 1
rt -= 1
else:
for _ in range(len(s) // 2 + 1):
if s[lt] != s[rt]:
return False
lt += 1
rt -= 1
return True
print(Solution().isPalindrome("A man, a plan, a canal: Panama"))
Leave a comment