File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ํต์ฌ ์์ด๋์ด:
3+ * 1. ์ ๊ทํํ์์ผ๋ก ์ํ๋ฒณ๊ณผ ์ซ์๋ง ์ถ์ถํ์ฌ ์๋ฌธ์๋ก ๋ณํ
4+ * 2. Two Pointer ๋ฐฉ์์ผ๋ก ์ ๋์์ ์ค์์ผ๋ก ์ด๋ํ๋ฉฐ ๋น๊ต
5+ *
6+ * @param {string } s - ๊ฒ์ฌํ ๋ฌธ์์ด
7+ * @return {boolean } - ํฐ๋ฆฐ๋๋กฌ ์ฌ๋ถ
8+ *
9+ * ์๊ฐ ๋ณต์ก๋: O(n)
10+ * - replace() ๋ฉ์๋: O(n) - ๋ฌธ์์ด ์ ์ฒด ์ํ
11+ * - toLowerCase(): O(n) - cleaned ๋ฌธ์์ด ์ํ
12+ * - while ๋ฃจํ: O(n/2) โ O(n) - ์ต๋ ๋ฌธ์์ด ๊ธธ์ด์ ์ ๋ฐ๋งํผ ๋ฐ๋ณต
13+ * - ์ ์ฒด: O(n) + O(n) + O(n) = O(n)
14+ *
15+ * ๊ณต๊ฐ ๋ณต์ก๋: O(n)
16+ * - cleaned ๋ฌธ์์ด: O(n) - ์ต์
์ ๊ฒฝ์ฐ ์
๋ ฅ ๋ฌธ์์ด์ ๋ชจ๋ ๋ฌธ์๊ฐ ์ํ๋ฒณ/์ซ์
17+ * - left, right ํฌ์ธํฐ ๋ณ์: O(1)
18+ * - ์ ์ฒด: O(n)
19+ */
20+ const isPalindrome = ( s ) => {
21+ // ์ํ๋ฒณ๊ณผ ์ซ์๋ง ๋จ๊ธฐ๊ณ ์๋ฌธ์๋ก ๋ณํ
22+ // \W๋ ์ํ๋ฒณ, ์ซ์, ์ธ๋์ค์ฝ์ด๋ฅผ ์ ์ธํ ๋ชจ๋ ๋ฌธ์
23+ // _๋ ์ ๊ฑฐํด์ผ ํ๋ฏ๋ก [^a-zA-Z0-9] ์ฌ์ฉ
24+ const cleaned = s . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '' ) . toLowerCase ( ) ;
25+
26+ // Two Pointer ์ ๊ทผ๋ฒ
27+ let left = 0 ;
28+ let right = cleaned . length - 1 ;
29+
30+ // ์ ๋์์ ์ค์์ผ๋ก ์ด๋ํ๋ฉฐ ๋น๊ต
31+ while ( left < right ) {
32+ if ( cleaned [ left ] !== cleaned [ right ] ) {
33+ return false ;
34+ }
35+ left ++ ;
36+ right -- ;
37+ }
38+
39+ return true ;
40+ } ;
You canโt perform that action at this time.
0 commit comments