-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd-1-encode-and-decode-strings.js
More file actions
41 lines (36 loc) · 1.01 KB
/
Copy pathd-1-encode-and-decode-strings.js
File metadata and controls
41 lines (36 loc) · 1.01 KB
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
34
35
36
37
38
39
40
41
class Codec {
encode(strs) {
let result = '';
for (let s of strs) {
result += s.length + '#' + s;
}
return result;
}
decode(str) {
let result = [];
let i = 0;
while (i < str.length) {
let j = i;
while (str[j] !== '#') {
j++;
}
let length = parseInt(str.substring(i, j));
result.push(str.substring(j + 1, j + 1 + length));
i = j + 1 + length;
}
return result;
}
}
// Test
const codec = new Codec();
const input = ['hello', 'world', 'leet', 'code'];
const encoded = codec.encode(input);
console.log('Encoded:', encoded); // "5#hello5#world4#leet4#code"
const decoded = codec.decode(encoded);
console.log('Decoded:', decoded); // ["hello", "world", "leet", "code"]
// Edge case - string with # inside
const input2 = ['he#llo', 'wor#ld'];
const encoded2 = codec.encode(input2);
console.log('Encoded:', encoded2); // "6#he#llo6#wor#ld"
const decoded2 = codec.decode(encoded2);
console.log('Decoded:', decoded2); // ["he#llo", "wor#ld"]