Skip to content

Commit e1d0070

Browse files
committed
week 3 - valid palindrome
1 parent 9c8fe48 commit e1d0070

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

valid-palindrome/liza0525.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
# Alphanumeric 스트링 이외의 문자열을 먼저 제외 시킨 이후 lowercase로 변경한 다음, 양 끝에서부터 문자열을 비교한다.
3+
def isPalindrome(self, s: str) -> bool:
4+
only_alnum_s = ''
5+
for ss in s:
6+
if ss.isalnum():
7+
# Alphanumeric이 맞다면 only_alnum_s에 문자열을 더한다.
8+
only_alnum_s += ss
9+
10+
# only_alnum_s 내의 모든 문자를 lowercase로 변경한다.
11+
only_alnum_s = only_alnum_s.lower()
12+
13+
for i in range(len(only_alnum_s) // 2):
14+
# string을 탐색할 index를 0부터 문자열의 반절까지 for문을 돌리며
15+
# 양 끝에서부터 i번 인덱스와 (len(only_alnum) - 1 - i)번 인덱스의 문자 값을 비교한다.
16+
# 같지 않은 경우에는 False로 early return해준다.
17+
if only_alnum_s[i] != only_alnum_s[len(only_alnum_s) - 1 - i]:
18+
return False
19+
20+
return True

0 commit comments

Comments
 (0)