File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * 문제 유형
3+ * - String
4+ *
5+ * 문제 설명
6+ * - 문자열 인코딩과 디코딩
7+ *
8+ * 아이디어
9+ * 1) "길이 + # + 문자열" 형태로 인코딩
10+ *
11+ */
12+ class Solution {
13+ /**
14+ * @param {string[] } strs
15+ * @returns {string }
16+ */
17+ encode ( strs ) {
18+ return strs . map ( ( str ) => `${ str . length } #${ str } ` ) . join ( "" ) ;
19+ }
20+
21+ /**
22+ * @param {string } str
23+ * @returns {string[] }
24+ */
25+ decode ( str ) {
26+ const result = [ ] ;
27+ let tempStr = str ;
28+ while ( tempStr . length ) {
29+ let i = 0 ;
30+
31+ while ( tempStr [ i ] !== "#" ) {
32+ i ++ ;
33+ }
34+
35+ const length = Number ( tempStr . slice ( 0 , i ) ) ;
36+ const currentStr = tempStr . slice ( i + 1 , i + 1 + length ) ;
37+ result . push ( currentStr ) ;
38+ tempStr = tempStr . slice ( length + i + 1 ) ;
39+ }
40+ return result ;
41+ }
42+ }
You can’t perform that action at this time.
0 commit comments