Back to course home
0% completed
Vote For New Content
Encode and Decode Strings (medium)
Problem Statement
Design an algorithm to implement these two functions: one that can effectively convert the list of strings to a single string (encoding) and another that can revert this single string back to the original list (decoding).
Constraints:
- You cannot use any serialization methods such as
eval
. - The goal is to ensure that after encoding and then decoding, the output is identical to the input.
Machine 1 (Sender):
encode(List<String> strs)
: Takes a list of strings and converts it to a single encoded string.
Machine 2 (Receiver):
decode(String s)
: Takes the encoded string and reconstructs the original list of strings.
Ensure that after sending the encoded string from Machine 1 to Machine 2, the list of strings obtained on Machine 2 is exactly the same as the one sent from Machine 1.
Examples
-
Example 1:
- Input:
strs = ["dog", "cat", "bird"]
- Expected Output:
["dog", "cat", "bird"]
- Justification: The list of strings contains three simple words. The encoded version should accurately store each word, and the decode function should reconstruct the exact original list.
- Input:
-
Example 2:
- Input:
strs = ["hello,world", "foo!bar", ""]
- Expected Output:
["hello,world", "foo!bar", ""]
- Justification: This test case includes strings with special characters like commas and exclamation marks, as well as an empty string. The encoding must handle special characters and empty strings properly without losing any data.
- Input:
-
Example 3:
- Input:
strs = ["", "", "empty"]
- Expected Output:
["", "", "empty"]
- Justification: This test case includes multiple empty strings and a non-empty string. The encoding should differentiate between multiple empty strings and preserve the original order and count of the strings.
- Input:
Constraints:
- 1 <= strs.length <= 200
- 0 <= strs[i].length <= 200
- strs[i] contains any possible characters out of 256 valid ASCII characters.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible