Design an algorithm to encode a list of strings to a single string. The encoded string is then decoded back to the original list of strings.
You should implement two methods:
class Codec:
def encode(self, strs: List[str]) -> str:
# Encodes a list of strings to a single string.
def decode(self, s: str) -> List[str]:
# Decodes a single string to a list of strings.
strs.length <= 1000
0 <= strs[i].length <= 200
strs[i]
contains any possible characters, including '/'
, digits, and whitespace.codec = Codec()
original = ["lint", "code", "love", "you"]
encoded = codec.encode(original)
decoded = codec.decode(encoded)
assert decoded == original
Avoid using characters like #
or /
as separators unless you handle escaping properly.
A robust way is to prefix each string with its length.