Skip to content

Commit 5c535db

Browse files
committed
feat(soobing): week5 > encode-and-decode-strings
1 parent 6a35335 commit 5c535db

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

encode-and-decode-strings/soobing2.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

0 commit comments

Comments
 (0)