Encode and Decode Strings

Problem

Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.

Machine 1 (sender) has the function:

String encode(List<String> strs) {
    // ... your code
    return encoded_string;
}

Machine 2 (receiver) has the function:

List<String> decode(String encoded_string) {
    // ... your code
    return decoded_strs;
}

So Machine 1 does:

String encoded_string = encode(strs);

and Machine 2 does:

List<String> decoded_strs = decode(encoded_string);

decoded_strs in Machine 2 should be the same as the input strs in Machine 1.

Implement the encode and decode methods.

Examples

Example 1:

Input: strs = ["Hello","World"]

Output: ["Hello","World"]

Example 2:

Input: strs = [""]

Output: [""]

Constraints

  • 0 <= strs.length < 100
  • 0 <= strs[i].length < 200
  • strs[i] contains any possible characters out of 256 valid ASCII characters.

You should aim for a solution with O(m) time for each encode() and decode() call and O(m+n) space, where m is the sum of lengths of all the strings and n is the number of strings.

Solution

We use special token for delim. Each string gets encoded into its size, the delim, then the string. Decoding reverses this process.

class Solution {
    constexpr static char DELIM = '#';
public:
 
    string encode(vector<string>& strs) {
        std::stringstream ss_result;
 
        // Each string gets converted to num of chars, delim, then payload
        for (const auto &str : strs) {
            ss_result << str.size();
            ss_result << DELIM;
            ss_result << str;
        }
 
        return ss_result.str();
    }
 
    vector<string> decode(const string &s) {
        std::vector<string> result;
        // This is not very efficient
        std::size_t idx = 0;
        while (idx < s.size()) {
            // Read str size
            std::size_t size = 0;
            while (s[idx] != DELIM) {
                size = size * 10 + (s[idx] - '0');
                ++idx;
            }
            // Read the delim
            // assert(s[idx] == DELIM);
            ++idx;
            // Read the string
            result.push_back(s.substr(idx, size));
            // Increment starting index
            idx += size;
        }
        return result;
    }
};