Problem
Design a way to send a list of strings as one string. The sender changes the list into one string. The receiver changes that string into the original list.
Write two functions. encode(strs) gets a list of strings strs and returns one string. decode(s) gets a string s from encode and returns the original list. The tests call decode(encode(strs)) and compare the result with strs.
A string in the list can contain any character with a code from 0 to 255. A separator is a character that marks where one string ends. No single character is safe as a separator, because any string can contain it. The list can contain empty strings, and the list can be empty.
Serialization changes data into a form that you can store or send. Do not use a library function for serialization, for example json.dumps, pickle or eval.
Example 1
- Input:
- strs = ["sun","","a#b"]
- Output:
- ["sun","","a#b"]
- Explanation:
- The decoded list is equal to the input. It keeps the empty string and the
#character.
Example 2
- Input:
- strs = ["4#x","12"]
- Output:
- ["4#x","12"]
- Explanation:
- The strings contain digits and
#. A correct solution still decodes each string with no change.
Example 3
- Input:
- strs = []
- Output:
- []
- Explanation:
- The result for an empty list is an empty list.
Constraints
- 0 <= strs.length <= 200
- 0 <= strs[i].length <= 200
- strs[i] contains only characters with codes from 0 to 255.
Follow-up
Make your solution work for any set of characters, not only the codes from 0 to 255.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
OpenAI
Leetcode 271. Encode and Decode Strings
Reported Mar 20, 2026 · 3 source reports
INTERN, MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View source