Practice/OpenAI/Leetcode 271. Encode and Decode Strings
CodingOptional
Create a codec (encoder/decoder) that can transform an array of strings into a single string representation and then reconstruct the original array from that representation. Your implementation must handle all possible string content, including empty strings, strings containing any Unicode characters, and strings that might contain sequences that look like delimiters or control characters.
The key challenge is that the strings in the input array can contain any characters, so you cannot simply use a delimiter like a comma or semicolon, as those characters might appear in the input strings themselves. Your encoding scheme must be lossless — decoding must always produce exactly the original array.
encode method that takes a list of strings and returns a single encoded stringdecode method that takes an encoded string and returns the original list of stringsdecode(encode(strings)) must always equal strings0 <= strings.length <= 10000 <= strings[i].length <= 1000Example 1:
Input: ["hello", "world"] Output: ["hello", "world"] Explanation: After encoding to a single string and then decoding, we get back the original array
Example 2:
Input: ["a", "", "b"] Output: ["a", "", "b"] Explanation: Empty strings must be preserved in their correct positions
Example 3:
Input: ["hello:world", "test,string", "special#chars"] Output: ["hello:world", "test,string", "special#chars"] Explanation: Common delimiter characters appearing in strings must not break the encoding
Example 4:
Input: [] Output: [] Explanation: An empty array should encode and decode to an empty array