Encode a list of strings into one string and decode it back exactly

easyAbout 20 minutesUpdated CodingProstringsserializationparsing
Not solved

Share this question

Problem

Two systems can only exchange a single string, but you need to send a whole list of strings. Write encode(strings), which turns an array of strings into one string, and decode(text), which turns that string back into the exact same array.

const packed = encode(["hello", "world"]);
decode(packed); // ["hello", "world"]

decode(encode(["a,b", "", "#3#", "🙂"])); // ["a,b", "", "#3#", "🙂"]
decode(encode([]));   // []
decode(encode([""])); // [""]

Ref. UR-1422 · © UIRound · uiround.com/questions/encode-a-list-of-strings-into-one-string-and-decode-it-back-exactly

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment