Design encode (list of strings → one string) and decode (string → original list) so that decode(encode(strs)) == strs for any strings.
Write each string as <length>#<string>. The decoder reads digits up to '#', then takes exactly that many characters, so any character (even '#') is safe inside a string.
1function encode(strs: string[]): string {2return strs.map((s) => s.length + "#" + s).join("");3}4function decode(data: string): string[] {5const out: string[] = [];6let i = 0;7while (i < data.length) {8const hash = data.indexOf("#", i);9const len = Number(data.slice(i, hash));10out.push(data.slice(hash + 1, hash + 1 + len));11i = hash + 1 + len;12}13return out;14}
4#neet
Encode "neet" as 4#neet.
Space: play/pause · ←/→: step