LeetCode 271 ·
Medium
Encode and Decode Strings
Design encode/decode so a list of strings survives a round-trip through a single string.
Try it
Step through the core mechanic. The simulator below runs the string matching shape this problem is built on.
Walk the pattern
No dedicated step-through for this one yet. The shape is String matching — its pattern page has the interactive walkthrough, the reference implementation, and a five-problem progression that this problem sits inside.
The approach
Length-prefix each string: write len, a delimiter, then the bytes ("5#hello"). Decoding reads the length, then takes exactly that many characters, so delimiters inside the payload are harmless.
| Aspect | Value |
|---|---|
| Pattern | String matching |
| Recognise it by | Serialize a list of arbitrary strings unambiguously. |
| Time complexity | O(n) |
| Space complexity | O(n) |
| Difficulty | Medium |
Who asks it
Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.