File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * [Problem]: [125] Valid Palindrome
3
+ *
4
+ * (https://leetcode.com/problems/valid-palindrome/description/)
5
+ */
6
+
7
+ function isPalindrome ( s : string ) : boolean {
8
+ // 시간복잡도: O(n)
9
+ // 공간복잡도: O(n)
10
+ function twoPointerFunc ( s : string ) : boolean {
11
+ let stringArr : string [ ] = [ ...s . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, "" ) ] ;
12
+ let left = 0 ;
13
+ let right = stringArr . length - 1 ;
14
+
15
+ while ( left < right ) {
16
+ if ( stringArr [ left ] . toLowerCase ( ) === stringArr [ right ] . toLowerCase ( ) ) {
17
+ left ++ ;
18
+ right -- ;
19
+ } else {
20
+ return false ;
21
+ }
22
+ }
23
+
24
+ return true ;
25
+ }
26
+
27
+ // 시간복잡도: O(n)
28
+ // 공간복잡도: O(1)
29
+ function optimizedTwoPointerFunc ( s : string ) : boolean {
30
+ let left = 0 ;
31
+ let right = s . length - 1 ;
32
+
33
+ while ( left < right ) {
34
+ while ( left < right && ! isLetterOrDigit ( s [ left ] ) ) left ++ ;
35
+ while ( left < right && ! isLetterOrDigit ( s [ right ] ) ) right -- ;
36
+
37
+ if ( s [ left ] . toLowerCase ( ) !== s [ right ] . toLowerCase ( ) ) return false ;
38
+ left ++ ;
39
+ right -- ;
40
+ }
41
+
42
+ return true ;
43
+
44
+ function isLetterOrDigit ( c : string ) : boolean {
45
+ return / [ a - z A - Z 0 - 9 ] / . test ( c ) ;
46
+ }
47
+ }
48
+ return optimizedTwoPointerFunc ( s ) ;
49
+ }
You can’t perform that action at this time.
0 commit comments