Encode and Decode Strings

Design encode(list of strings) → one string, and decode(that string) → the original list. The strings may contain any character.

A way to picture it

You're shipping parcels on one conveyor belt. A plain separator fails the moment a parcel contains the separator. Instead, stamp each parcel's exact size on its label. The receiver reads the label, then grabs exactly that many characters — no guessing.

How to approach it

Encode each word as length + '#' + word. Decode by reading digits up to '#', then taking exactly that many characters.

Example

Input: {"strs":["neet","code","love","you"]}. Output: ["neet","code","love","you"].

Complexity: O(total characters) time and O(total characters) space. Every character is written once and read once.

This page includes an animated walkthrough, Java and Python solutions, and a question to check your understanding.