File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Source: https://leetcode.com/problems/valid-palindrome/
3
+ * ํ์ด๋ฐฉ๋ฒ: ๋ฌธ์์ด์ ์กฐ๊ฑด์ ๋ง๊ฒ ์ ์ ํ ํ reverseํ์ฌ ๋น๊ต
4
+ * ์๊ฐ๋ณต์ก๋: O(n)
5
+ * ๊ณต๊ฐ๋ณต์ก๋: O(n)
6
+ *
7
+ * ์๊ฐ๋๋ ํ์ด๋ฐฉ๋ฒ
8
+ * 1. ์ ๊ท์์ ์ด์ฉํ์ฌ ๋ฌธ์์ด์ ์ ์ ํ ํ reverseํ์ฌ ๋น๊ต => ์ ๊ท์ ์์ฑ์ ๋ชปํด์ ๋ฐฐ์
9
+ * 2. ๋ฌธ์์ด์ ์ํํ๋ฉด์ ์กฐ๊ฑด์ ๋ง๋ ๋ฌธ์๋ง ๋ฐฐ์ด์ ์ ์ฅํ ํ reverseํ์ฌ ๋น๊ต
10
+ */
11
+ function isPalindrome ( s : string ) : boolean {
12
+ // ๋ฏธ๋ฆฌ ๊ธธ์ด๊ฐ 2๋ณด๋ค ์์ ๊ฒฝ์ฐ๋ true๋ก ๋ฐํ(Palindrome ์กฐ๊ฑด์ ๋ง์)
13
+ if ( s . length < 2 ) return true ;
14
+ const formatted : string [ ] = [ ] ;
15
+
16
+ // ๋ฌธ์์ด์ ์ํํ๋ฉด์ ์กฐ๊ฑด์ ๋ง๋ ๋ฌธ์๋ง ์๋ฌธ์๋ก ๋ณํํด์ ๋ฐฐ์ด์ ์ ์ฅ
17
+ for ( let c of s ) {
18
+ if (
19
+ ( c >= "a" && c <= "z" ) ||
20
+ ( c >= "A" && c <= "Z" ) ||
21
+ ( c >= "0" && c <= "9" )
22
+ )
23
+ formatted . push ( c . toLowerCase ( ) ) ;
24
+ }
25
+ // formatted ๋ฐฐ์ด์ reverseํ์ฌ JSON.stringify๋ก ๋น๊ต (์ค์)
26
+ // ? toReverse()๊ฐ chrome console์์๋ ์๋ํ๋๋ฐ ์ฌ๊ธฐ์๋ ์ ์๋๋์ง ๋ชจ๋ฅด๊ฒ ์
27
+ const reversed = [ ...formatted ] . reverse ( ) ;
28
+ return JSON . stringify ( reversed ) === JSON . stringify ( formatted ) ;
29
+ }
You canโt perform that action at this time.
0 commit comments