20 · String Parsing & Expression Evaluation

#271 Encode and Decode Strings

frequently reported atGoogleAmazonMeta

the problem

In plain words

Design a Codec with encode(List<String> strs), which packs a list of strings into one String, and decode(String s), which unpacks it into the same list. The packed String may be decoded by a different Codec object, so it must carry everything. The strings can contain any of the 256 ASCII characters — including whatever separator you pick. Don't use a ready-made serializer.

in strs = ["Hello","World"]

out ["Hello","World"]

One valid encoding: "5#Hello5#World".

in strs = [""]

out [""]

One empty string must come back as one empty string, not as an empty list.

  • 1 ≤ strs.length ≤ 200
  • 0 ≤ strs[i].length ≤ 200
  • strs[i] may contain any of the 256 ASCII characters

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code right here. Stuck for 20 minutes? Open one hint at a time.

This one is LeetCode Premium. You don't need a subscription: solve it right here. The free alternative linked above is the closest free practice; its label says how close it is.

Solve it in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #297
    2. #394
      Decode String
      Mediumin Stacks
    3. #8
      String to Integer (atoi)
      Mediumin Parsing