forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhsskey.js
More file actions
33 lines (28 loc) · 667 Bytes
/
hsskey.js
File metadata and controls
33 lines (28 loc) · 667 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
/**
* @param {string[]} strs
* @returns {string}
*/
encode(strs) {
return strs.map((item) => `${item.length}#${item}`).join('');
}
/**
* @param {string} str
* @returns {string[]}
*/
decode(str) {
const result = [];
let i = 0;
while (i < str.length) {
let j = i;
while (str[j] !== '#') {
j++;
}
const length = parseInt(str.slice(i, j));
const word = str.slice(j + 1, j + 1 + length);
result.push(word);
i = j + 1 + length;
}
return result;
}
}