File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ /**
3+ * ๋ฌธ์์ด ๋ฐฐ์ด์ ํ๋์ ๋ฌธ์์ด๋ก ์ธ์ฝ๋ฉํฉ๋๋ค.
4+ * @param strs - ๋ฌธ์์ด ๋ฐฐ์ด
5+ * @returns ์ธ์ฝ๋ฉ๋ ํ๋์ ๋ฌธ์์ด
6+ */
7+ encode ( strs : string [ ] ) : string {
8+ return strs . map ( ( str ) => str . length + "#" + str ) . join ( "" ) ;
9+ }
10+
11+ /**
12+ * ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด์ ์๋ ๋ฌธ์์ด ๋ฐฐ์ด๋ก ๋์ฝ๋ฉํฉ๋๋ค.
13+ * @param str - ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด
14+ * @returns ๋์ฝ๋ฉ๋ ๋ฌธ์์ด ๋ฐฐ์ด
15+ */
16+ decode ( str : string ) : string [ ] {
17+ const result : string [ ] = [ ] ;
18+
19+ let i = 0 ;
20+ while ( i < str . length ) {
21+ let j = i ;
22+ while ( str [ j ] !== "#" ) {
23+ j ++ ;
24+ }
25+
26+ const length = parseInt ( str . slice ( i , j ) ) ;
27+ const word = str . slice ( j + 1 , j + 1 + length ) ;
28+ result . push ( word ) ;
29+ i = j + 1 + length ;
30+ }
31+
32+ return result ;
33+ }
34+ }
You canโt perform that action at this time.
0 commit comments