Encode and Decode Strings
medium
arrays
strings
design
Design an algorithm to encode a list of strings into a single string, and decode that single string back into the exact original list of strings.
The input strings may contain any character — including spaces, empty strings, #, digits, newlines, or Unicode — so a naive delimiter split will break. Implement encode and decode and return decode(encode(strs)) from encode_and_decode.
Input / output
- Input:
strs: string[] - Output:
string[]— the exact decoded list produced bydecode(encode(strs))
Constraints
0 <= strs.length <= 2000 <= strs[i].length <= 200strs[i]may contain any valid UTF-8 / ASCII character, including#and digits
Follow-up
Why does length-prefixing (<len>#<payload>) beat escaping a delimiter character when strings contain nested escape sequences or binary data?
Examples
Example 1
Input: strs = ["lint","code","love","you"]
Output: ["lint","code","love","you"]
Example 2
Input: strs = ["we","say",":","yes","!@#$%^&*()"]
Output: ["we","say",":","yes","!@#$%^&*()"]
Example 3: single empty string
Input: strs = [""]
Output: [""]