Question

Formatted question description: https://leetcode.ca/all/271.html

271. Encode and Decode Strings

Level

Medium

Description

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(vector<string> strs) {
  // ... your code
  return encoded_string;
}

Machine 2 (receiver) has the function:

vector<string> decode(string s) {
  //... your code
  return strs;
}

So Machine 1 does:

string encoded_string = encode(strs);

and Machine 2 does:

vector<string> strs2 = decode(encoded_string);

strs2 in Machine 2 should be the same as strs in Machine 1.

Implement the encode and decode methods.

Note:

  • The string may contain any possible characters out of 256 valid ascii characters. Your algorithm should be generalized enough to work on any possible characters.
  • Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
  • Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm.

Solution

In encode() method, store each string’s length so that the strings can be uniquely determined. For each string str in strs, encode it as the following format: str.length() + "/" + str.

  • For example, “encode” is encoded as 6/encode. All strings’ encoding results are concatenated together.

In decode() method, each time find the first index of "/", and the substring before "/" is an integer that represents the length of the next string. Convert the substring before "/" into an integer, obtain the substring with the determined length just after "/", and add the substring after "/" to the result list. After one string is decoded, continue with the remaining strings. Finally, return the result list.

You can also use a simpler compression method, add a newline character \0 at the end of each string, which also belongs to a string, so when decoding, just look for the newline character.

Code

Java

  • import org.apache.commons.io.IOUtils;
    
    import java.io.IOException;
    import java.io.StringReader;
    import java.util.LinkedList;
    import java.util.List;
    
    public class Encode_and_Decode_Strings {
    
        public class Codec {
    
            // Encodes a list of strings to a single string.
            public String encode(List<String> strs) {
                String res = "";
                for (String str : strs) res += str + '\0';
                return res;
            }
    
            // Decodes a single string to a list of strings.
            public List<String> decode(String s) throws IOException {
                return IOUtils.readLines(new StringReader(s));
            }
        }
    
        public class Codec_2nd_solution {
    
            // Encodes a list of strings to a single string.
            public String encode(List<String> strs) {
                StringBuffer sb = new StringBuffer();
                int size = strs.size();
                for (int i = 0; i < size; i++) {
                    String str = strs.get(i);
                    int length = str.length();
                    sb.append(length + "/" + str);
                }
                return sb.toString();
            }
    
            // Decodes a single string to a list of strings.
            public List<String> decode(String s) {
                List<String> list = new ArrayList<String>();
                StringBuffer sb = new StringBuffer(s);
    
                int endIndex = 0;
                while (endIndex < s.length()) {
                    int index = sb.toString().indexOf('/', endIndex); // @note: first `/` after index i
                    int length = Integer.parseInt(sb.substring(endIndex, index));
                    endIndex = index + length + 1;
                    String str = sb.substring(index + 1, endIndex);
                    list.add(str);
                }
                return list;
            }
        }
    
    // Your Codec object will be instantiated and called as such:
    // Codec codec = new Codec();
    // codec.decode(codec.encode(strs));
    
    // Your Codec object will be instantiated and called as such:
    // Codec codec = new Codec();
    // codec.decode(codec.encode(strs));
    
    }
    
    
  • // OJ: https://leetcode.com/problems/encode-and-decode-strings/
    // Time: O(N)
    // Space: O(1)
    class Codec {
    public:
        string encode(vector<string>& strs) {
            string ans;
            for (string &str : strs) {
                for (char c : str) {
                    if (c == '$') ans.push_back(c);
                    ans.push_back(c);
                }
                ans.push_back('$');
                ans.push_back('x');
            }
            return ans;
        }
        vector<string> decode(string s) {
            vector<string> ans;
            int i = 0, N = s.size();
            while (i < N) {
                string str;
                for (; i < N; ++i) {
                    if (s[i] != '$') str.push_back(s[i]);
                    else if (i + 1 < N && s[i + 1] == '$') str.push_back(s[i++]);
                    else {
                        i += 2;
                        break;
                    }
                }
                ans.push_back(str);
            }
            return ans;
        }
    };
    
  • class Codec:
      def encode(self, strs):
        """Encodes a list of strings to a single string.
        
        :type strs: List[str]
        :rtype: str
        """
        ret = ""
        for s in strs:
          ret += str(len(s)) + "|" + s
    
        return ret
    
      def decode(self, s):
        """Decodes a single string to a list of strings.
        
        :type s: str
        :rtype: List[str]
        """
        ret = []
        start = end = 0
        while end < len(s):
          if s[end] != "|":
            end += 1
          else:
            length = int(s[start:end])
            start = end + 1
            end = start + length
            ret.append(s[start:end])
            start = end
        return ret
    
    # Your Codec object will be instantiated and called as such:
    # codec = Codec()
    # codec.decode(codec.encode(strs))
    
    

All Problems

All Solutions