File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ๋ฌธ์์ด ๋ฆฌ์คํธ๋ฅผ ํ๋์ ๋ฌธ์์ด๋ก ์ธ์ฝ๋ฉํ๊ณ , ๋ค์ ๋์ฝ๋ฉ ํ๊ธฐ
3
+ *
4
+ * ์ ๊ทผ ๋ฐฉ๋ฒ:
5
+ * ๊ธธ์ด ๊ธฐ๋ฐ ์ธ์ฝ๋ฉ => ๊ฐ ๋ฌธ์์ด๋ง๋ค "๊ธธ์ด#๋ฌธ์์ด" ํ์์ผ๋ก ์ธ์ฝ๋ฉ
6
+ * "#"์ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด์ ๋๋๊ณ , ๊ธธ์ด๋ฅผ ์ด์ฉํด ์๋ ๋ฌธ์์ด์ ๋ณต์ํ์ฌ ๋์ฝ๋ฉ
7
+ */
8
+
9
+ /**
10
+ * @param {string[] } strs - ์ธ์ฝ๋ฉํ ๋ฌธ์์ด ๋ฆฌ์คํธ
11
+ * @return {string } - ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด
12
+ */
13
+ var encode = function ( strs ) {
14
+ if ( ! strs || strs . length === 0 ) return '' ;
15
+ return strs . map ( ( str ) => str . length + '#' + str ) . join ( '' ) ;
16
+ } ;
17
+
18
+ /**
19
+ * @param {string } s - ๋์ฝ๋ฉํ ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด
20
+ * @return {string[] } - ์๋์ ๋ฌธ์์ด ๋ฆฌ์คํธ
21
+ */
22
+ var decode = function ( s ) {
23
+ if ( ! s || s . length === 0 ) return [ ] ;
24
+
25
+ const result = [ ] ;
26
+ let i = 0 ;
27
+
28
+ while ( i < s . length ) {
29
+ const delimiterIndex = s . indexOf ( '#' , i ) ; // '#'์ ์์น ์ฐพ๊ธฐ
30
+ const length = parseInt ( s . slice ( i , delimiterIndex ) ) ; // ๊ธธ์ด ์ถ์ถ
31
+
32
+ const start = delimiterIndex + 1 ; // ๋ฌธ์์ด ์์ ์์น
33
+ result . push ( s . slice ( start , start + length ) ) ; // ์์๋ธ ๊ธธ์ด๋งํผ ๋ฌธ์์ด ์ถ์ถ ํ ๊ฒฐ๊ณผ์ ์ถ๊ฐ
34
+ i = start + length ; // ๋ค์ ๋ฌธ์์ด ์์ ์์น๋ก ํฌ์ธํฐ ์ด๋
35
+ }
36
+
37
+ return result ;
38
+ } ;
You canโt perform that action at this time.
0 commit comments