Back to Practice Dashboard
Top 150 InterviewEasy

Encode and Decode Strings

Learn how to solve the 'Encode and Decode Strings' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

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.

Implement two functions:

- encode(strs: List[str]) -> str — Encodes a list of strings to a single string.

- decode(s: str) -> List[str] — Decodes a single string back to the original list of strings.

The encoded string should be able to handle any possible characters in the input strings, including special characters and empty strings.

Constraints
  • 0 <= len(strs) <= 200
  • 0 <= len(strs[i]) <= 200
  • strs[i] can contain any possible characters (0-255)

Examples

Example 1
Input
strs = ["hello", "world"]
Output
["hello", "world"]
Explanation

The list is encoded into a single string and then decoded back to the original list ["hello", "world"].

Example 2
Input
strs = [""]
Output
[""]
Explanation

A list containing a single empty string is encoded and decoded correctly.

Example 3
Input
strs = ["we", "say", ":", "yes"]
Output
["we", "say", ":", "yes"]
Explanation

Special characters like ":" are handled correctly during encoding and decoding.

Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor