File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed
Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change 1+ // ์๊ฐ ๋ณต์ก๋: O(n)
2+ // ๊ณต๊ฐ ๋ณต์ก๋: O(n)
3+ function isPalindrome ( s : string ) : boolean {
4+ //1. s์์ ๋ฌธ์์ ์ซ์๊ฐ ์๋ ๋ค๋ฅธ ๊ฒ๋ค์ ์ ๊ฑฐ
5+ //2. ๋๋ฌธ์ -> ์๋ฌธ์, ๋์ด์ฐ๊ธฐ, ๊ณต๋ฐฑ ์ ๊ฑฐ
6+ let originalString = s ;
7+ let alphabeticString = originalString . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '' ) ;
8+ const filteredString = alphabeticString . toLowerCase ( ) . trim ( ) ;
9+
10+ //3. ์ ๊ฑฐ ํ length === 0 ์ผ ๊ฒฝ์ฐ true๋ก ๊ฒฐ๊ณผ ๊ฐ ๋ฐํ.
11+ if ( filteredString . length === 0 ) {
12+ return true ;
13+ }
14+
15+ let count = 0 ;
16+ let roundedStringLength = Math . round ( filteredString . length / 2 ) ;
17+ for ( let i = 0 ; i < roundedStringLength ; i ++ ) {
18+ // ์ ๋์ ๊ธ์๊ฐ ๋์ผํ์ง ์ฒดํฌํ๊ณ
19+ if ( filteredString [ i ] === filteredString [ filteredString . length - i - 1 ] ) {
20+ count += 1 ;
21+ }
22+ }
23+ return count === roundedStringLength ? true : false ;
24+ }
You canโt perform that action at this time.
0 commit comments