Encode and Decode Strings
Part of Practice: Hash Keys & Buckets
Design two functions: encode turns a list of strings into one single string, and decode turns that single string back into the original list, exactly. The catch is that the strings may contain any characters at all — including whatever character you might want to use as a separator — so a naive "join with a comma" scheme breaks.
Empty strings and an empty list must round-trip correctly too. This is the classic serialization problem: the encoded form must be unambiguous no matter what the inputs contain.
Examples
- Input:
- strs = ["neet", "code", "love", "you"]
- Output:
- ["neet", "code", "love", "you"]
encode produces one string such as "4#neet4#code4#love3#you"; decode recovers the original list exactly.
- Input:
- strs = ["we", "say", ":", "yes"]
- Output:
- ["we", "say", ":", "yes"]
The strings can contain punctuation like ':' — the scheme must not be confused by any character.
- Input:
- strs = ["", "ab"]
- Output:
- ["", "ab"]
An empty string must survive the round trip as an empty string, not disappear.
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.