Skip to content

Commit 7161650

Browse files
committed
feat: encode-and-decode-strings
1 parent 61e5e54 commit 7161650

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
/**
3+
* @param {string[]} strs
4+
* @returns {string}
5+
*/
6+
encode(strs) {
7+
return strs.map((str) => `${str.length}#${str}`).join('');
8+
}
9+
10+
/**
11+
* @param {string} str
12+
* @returns {string[]}
13+
*/
14+
decode(str) {
15+
const result: string[] = [];
16+
let i = 0;
17+
while(i < str.length) {
18+
const j = str.indexOf('#', i);
19+
const length = Number(str.slice(i, j));
20+
result.push(str.slice(j + 1, j + 1 + length));
21+
i = j + 1 + length;
22+
}
23+
return result;
24+
}
25+
}

0 commit comments

Comments
 (0)