Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Problem

Encode and Decode Strings

LeetCode #271Medium
Round-trip a list of strings · survive any character

Design encode(list<string>) → string and decode(string) → list<string> so that the original list is recovered exactly. The strings may contain ANY characters, including whatever you pick as a delimiter.

Asked atGoogleAmazon
step 1 / 20
lint
code
love
you
[0][1][2][3]
Naive delimiter (why it breaks)
1encode(words): return words.join("#")
2decode(s): return s.split("#")
3// FAILS: a word containing "#" is split apart
state
  • sep#

line 1First idea: join the words with a separator like "#", e.g. "lint#code#love#you", and split on "#" to decode. Simple — but it secretly assumes no word ever contains "#".