All problems

Encode and Decode Strings

Medium · string encoding

Two services talk over a channel that can carry exactly one string per message. One service has a list of strings to send. It has to pack the list into a single string (encode), and the other service has to unpack that string back into the original list (decode).

Design both halves inside your function, then return decode(encode(strs)). The list that comes back must be exactly strs: the same strings, in the same order, with the same number of items. Do not return strs itself: the point is that everything has to survive being squeezed into one string.

The strings can contain any printable ASCII character, including digits, spaces, commas and whatever character you might be tempted to use as a separator. Strings may be empty, and the list may be empty. An empty list and a list holding one empty string are different and must come back as different lists.

Examples

Example 1

Input: strs = ["hello", "world"]

Output: ["hello", "world"]

Two ordinary words go in and the same two words come out.

Example 2

Input: strs = ["a,b", "", "3#xy"]

Output: ["a,b", "", "3#xy"]

A comma, an empty string and something that looks like a length prefix all have to survive unchanged.

Example 3

Input: strs = []

Output: []

An empty list comes back as an empty list, not as a list holding one empty string.

Constraints