Skip to content
Decoder

Encode and Decode Strings

Medium

Problem

Write two functions. encode turns a list of strings into one string, and decode turns that string back into the original list. **Any characters may appear** in the strings, including #, commas, digits and empty strings, so a simple separator will break.

Example 1

Input
decode(encode(["hello", "world"]))
Output
["hello", "world"]

Example 2

Input
decode(encode(["a#b", "", "3#x"]))
Output
["a#b", "", "3#x"]

Example 3

Input
decode(encode([]))
Output
[]

You choose the encoded format, as long as decode(encode(strs)) == strs for every list.

Constraints

  • 0 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] may contain any character. Do not rely on any character being absent.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.