File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -24,3 +24,33 @@ def isPalindrome(self, s: str) -> bool:
2424 return False
2525
2626 return True
27+
28+
29+ # 7기 풀이
30+ # 시간 복잡도: O(n)
31+ # - 문자열 s의 전체 문자를 순회할 때 길이 n만큼의 시간 소요
32+ # 공간 복잡도: O(1)
33+ # - start, end 등의 변수만 사용
34+
35+ class Solution :
36+ def isPalindrome (self , s : str ) -> bool :
37+ # two pointer로 각 문자열을 비교하며 palindrome인지 체크한다.
38+ # 해당 문제의 조건 중 하나가 alphanumeric한 문자만 체크하는 것
39+ start , end = 0 , len (s ) - 1
40+
41+ # 양 끝의 포인터 인덱스가 같아지기 전까지 루프를 돌린다.
42+ while start < end :
43+ if not s [start ].isalnum ():
44+ # s[start] 문자가 alphanumeric하지 않은 경우엔 start 포인터를 오른쪽으로 한 칸 이동
45+ start += 1
46+ continue
47+ if not s [end ].isalnum ():
48+ # s[end] 문자가 alphanumeric하지 않은 경우엔 end 포인터를 왼쪽으로 한 칸 이동
49+ end -= 1
50+ continue
51+ if s [start ].lower () != s [end ].lower ():
52+ # 두 문자의 lowercase가 동일하지 않은 경우에는 palindrome이 아니므로 False로 early return 해준다.
53+ return False
54+ start , end = start + 1 , end - 1
55+ # while 루프가 다 돌았다면 조건에 걸리지 않고 palindrome임을 충족하므로 True를 return
56+ return True
You can’t perform that action at this time.
0 commit comments