File tree 1 file changed +36
-0
lines changed
1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {boolean }
4
+ */
5
+ var isPalindrome = function ( s ) {
6
+ // 1. ์๋ ๋ฐฉ์์ O(n^2)์ ์๋ ดํจ
7
+ // s์ ๊ตฌ์ฑ์์๋ค์ ์๋ฌธ์๋ก ๋ณํ ํ ๋ฐฐ์ด์ ๋ฃ๊ณ
8
+ // ํ์ฌ ๊ธธ์ด๊ฐ 2 ์ด์์ด๋ผ๋ฉด
9
+ // shift === pop ์ด๋ฉด true ์๋๋ฉด false
10
+
11
+ // 2. ํฌ ํฌ์ธํฐ ์ฌ์ฉ
12
+
13
+ let str = s . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] / g, "" ) ;
14
+
15
+ // ๋ ํฌ์ธํฐ ์ฌ์ฉ: ์์๊ณผ ๋์์๋ถํฐ ๋น๊ต
16
+ let left = 0 ;
17
+ let right = str . length - 1 ;
18
+
19
+ while ( left < right ) {
20
+ if ( str [ left ] !== str [ right ] ) {
21
+ return false ; // ๋ ๋ฌธ์๊ฐ ๋ค๋ฅด๋ฉด palindrome ์๋
22
+ }
23
+
24
+ left ++ ;
25
+ right -- ;
26
+ }
27
+
28
+ return true ;
29
+ } ;
30
+
31
+ /*
32
+ 1. ์๊ฐ๋ณต์ก๋: O(n)
33
+ - toLowerCase, replace, ํฌ์ธํฐ ์ฌ์ฉ์ O(n) ์์๋จ
34
+ 2. ๊ณต๊ฐ๋ณต์ก๋: O(n)
35
+ - ๋ณ์ str์ด O(n) ์ฐจ์งํจ
36
+ */
You canโt perform that action at this time.
0 commit comments