Encode and Decode Strings
Medium
Array
String
Design
Design an algorithm to **encode** a list of strings to a single string, and **decode** that string back to the original list of strings. Implement `encode(strs)` and `decode(s)` such that `decode(encode(strs))` always equals `strs`. Your encoding must work for strings containing **any** characters, including commas, quotes, and colons.
Example 1
Input: strs = ["neet","code","love","you"]
Output: ["neet","code","love","you"]
Explanation: decode(encode(strs)) returns the original list.
Example 2
Input: strs = ["we","say",":","yes"]
Output: ["we","say",":","yes"]

Constraints

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] contains any possible characters out of 256 valid ASCII characters.
Time Complexity
O(n)
Space Complexity
O(n)
14
Case 1
Input: ["neet","code","love","you"]
Expected: true
Case 2
Input: ["we","say",":","yes"]
Expected: true
Case 3
Input: [""]
Expected: true