Skip to main content

Encode and Decode Strings

Medium
Data Structures and Algorithms
Editorial
Topics:
Array
String
Design

You are given a list of strings strs, where each string may contain any of the 256 valid ASCII characters (including special characters and empty strings). Your task is to implement two methods:

  • encode(strs): Encodes the list of strings into a single string.
  • decode(s): Decodes the single string back into the original list of strings.

The encoded string produced by encode must contain all the information necessary to reconstruct the original list exactly, even if some strings are empty or contain special characters. You are not allowed to use any built-in serialization methods (such as eval).

Constraints:

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • Each strs[i] may contain any ASCII character.

Return the encoded string from encode, and the decoded list of strings from decode.

Need help or want more practice?

Practise in an interviewGet AI Coach help