Skip to content
BASA DSA

Encode and Decode Strings

MediumArrays & Hashing

Implement encode(strings) returning one string and decode(encoded) recovering the original string array. Choose any unambiguous format; empty strings and delimiter characters must survive a round trip.

Example 1

Input
strs = ["a#b","","12#","hello"]
Output
["a#b","","12#","hello"]

Explanation

Recover all four strings exactly, including embedded # characters, an empty string, and numeric prefixes.

Example 2

Input
strs = []
Output
[]

Explanation

An empty list must decode to no elements.

Example 3

Input
strs = ["",""]
Output
["",""]

Explanation

Two empty strings must remain two elements rather than collapse into an empty list.

Constraints

  • 0 <= number of strings <= 100
  • Each string contains at most 200 ASCII characters
  • decode receives a value produced by encode

Function contractencodeArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…