File tree 1 file changed +29
-6
lines changed
1 file changed +29
-6
lines changed Original file line number Diff line number Diff line change 3
3
- 1 <= s.length <= 2 * 10^5
4
4
- s consists only of printable ASCII characters.
5
5
6
- Time Complexity:
7
- - O(n)
8
- Space Complexity:
9
- - O(n)
6
+ <Solution 1>
7
+ Time Complexity: O(n)
8
+
9
+ Space Complexity: O(n)
10
10
"""
11
- # Solution 1
12
11
class Solution :
13
12
def isPalindrome (self , s : str ) -> bool :
14
13
s = re .sub (r'[^a-zA-z0-9]' , '' , s ).lower ()
15
14
if s == s [::- 1 ]:
16
15
return True
17
16
return False
17
+ """
18
+ <Solution 2>
19
+ Time Complexity: O(n)
20
+ - 팰린드롬일 경우, 각 문자를 한 번씩 검사
21
+
22
+ Space Complexity: O(1)
23
+ - left, right 포인터 외에 추가 공간 사용 없음
24
+ """
25
+ class Solution :
26
+ def isPalindrome (self , s : str ) -> bool :
27
+ left , right = 0 , len (s ) - 1
28
+
29
+ while left < right :
30
+ while left < right and not s [left ].isalnum ():
31
+ left += 1
32
+
33
+ while left < right and not s [right ].isalnum ():
34
+ right -= 1
35
+
36
+ if s [left ].lower () != s [right ].lower ():
37
+ return False
38
+
39
+ left += 1
40
+ right -= 1
18
41
19
- # Solution 2
42
+ return True
You can’t perform that action at this time.
0 commit comments