Encode a list of strings into one string and decode it back exactly
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
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
- 2
- code listings
- 5
- edge cases
What's inside
- The full problem, with examples
- Tests that check your solution
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Card worldwide
- UPI and net banking in India
- One-time payment, nothing renews
Stuck? Take a hint
- 1Joining with a separator such as ',' breaks as soon as a string contains that separator, and escaping gets fiddly. Ask instead: what would let the decoder know where each string ends without looking at its contents?
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.