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