File tree Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def isPalindrome (self , s : str ) -> bool :
3+ left = 0
4+ right = len (s ) - 1
5+
6+ while left < right :
7+ if not s [left ].isalnum ():
8+ left += 1
9+ continue
10+
11+ if not s [right ].isalnum ():
12+ right -= 1
13+ continue
14+
15+ if s [left ].upper () != s [right ].upper ():
16+ return False
17+
18+ left += 1
19+ right -= 1
20+
21+ return True
22+
23+
24+ """
25+ ================================================================================
26+ 풀이 과정
27+ ================================================================================
28+ - A man, a plan, a canal: Panama
29+ - 띄어쓰기는 무시하고 앞 뒤에서 똑같은지 체크를 해야하네?
30+ - 앞 포인터와 뒷 포인터에서 시작해서 띄어쓰기 만나면 건너뛰고
31+ - 틀린것 나오면 False 반환하고, False를 만난적 없으면 True 반환
32+
33+ [1차 시도] Two Pointer
34+ ────────────────────────────────────────────────────────────────────────────────
35+ 1. 접근 방법
36+ - Two Pointer 사용: left는 앞에서, right는 뒤에서 시작
37+ - 유효하지 않은 문자(알파벳/숫자가 아닌 것)는 건너뛰기
38+ - 유효한 문자끼리 비교하며 중앙으로 이동
39+
40+ 2. 구현
41+ left = 0
42+ right = len(s) - 1
43+
44+ while left < right:
45+ # 왼쪽 포인터: 알파벳/숫자가 아니면 건너뛰기
46+ if not s[left].isalpha() and not s[left].isnumeric():
47+ left += 1
48+ continue
49+
50+ # 오른쪽 포인터: 알파벳/숫자가 아니면 건너뛰기
51+ if not s[right].isalpha() and not s[right].isnumeric():
52+ right -= 1
53+ continue
54+
55+ # 대소문자 무시하고 비교
56+ if s[left].upper() != s[right].upper():
57+ return False
58+
59+ left += 1
60+ right -= 1
61+
62+ return True
63+
64+ 4. 시간 복잡도: O(n) - 문자열을 한 번만 순회
65+ 5. 공간 복잡도: O(1) - 추가 공간 사용 없음 (포인터 2개만 사용)
66+ """
You can’t perform that action at this time.
0 commit comments