InferenceAI

Enter at least two characters to search.

Start free

Encode and Decode Strings

mediumArrays & Strings25 min25 XP

Design an encoding that turns a list of strings into a single string and back, for any characters (including the delimiter you choose). Implement it with the length-prefix scheme: each string becomes len(s) + # + s. Return [encoded, decoded].

Examples

EXAMPLE WALKTHROUGHtwo words

A guided view of the published example, not a trace of your code.

INPUT[["hello","world"]]
TASKencode_decode_strings()
EXPECTED OUTPUT["5#hello5#world",["hello","world"]]

Start with the input values shown in this example.

Example 1 of 3

Input: strings = ["hello","world"]

Output: ["5#hello5#world",["hello","world"]]

Explanation: two words

Input: strings = ["a#b","","##"]

Output: ["3#a#b0#2###",["a#b","","##"]]

Explanation: delimiters inside

Input: strings = []

Output: ["",[]]

Explanation: empty list

Hint 1

Read the number up to '#', then take exactly that many characters; delimiters inside strings no longer matter.

Topics

DSAArrays & StringsPython