Two programs need to pass a list of strings between them, but the only thing that can travel from one to the other is a single string. Design that format.
Write the class Codec:
encode(strs) turns the list strs into one string and returns it.decode(s) takes a string made by encode and returns the original list: the same strings, in the same order, with the same characters.Only the round trip is checked. For each test, one new Codec object calls encode(strs), and a different new Codec object calls decode on the string that came back. The list decode returns must equal strs exactly. The format of the encoded string is entirely up to you. Because the two calls run on separate objects, encode cannot leave the list on its object for decode to pick up: everything decode needs has to be inside the string.
The strings can hold any printable character, including commas, quotes, backslashes, # and spaces, so no single character is safe to use as a plain separator. Build the format yourself rather than handing the list to a serialization library.
The empty string in the middle has to survive the trip, and so does the comma inside "a,b": joining the list with commas would turn it into four strings.
The strings are #, 3#xyz, say "hi" and c:\ (shown escaped, as in any string literal). Whatever markers your format uses can also appear inside the strings themselves.
A list holding one empty string is different from an empty list, so their encodings must differ too.
Constraints
0 <= strs.length <= 2000 <= strs[i].length <= 200strs[i] is printable ASCII, with a character code from 32 to 126: letters, digits, the space, and punctuation such as , " \ # : |.Any separator character you pick can also appear inside a string, so a plain join can't be split back reliably. Store something that tells decode where each string ends, instead of searching for a marker.
The first idea is to glue the strings together with a separator, a comma say, and split on it later. It breaks at once: "a,b" holds a comma, so it comes back as two strings. Picking a rarer character doesn't help, because the strings can hold any printable character, including whichever one you pick.
Time O(L + k)Space O(L + k)
The full walkthrough and code are part of Premium