271. Encode and Decode Strings

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 strs) {
// ... your code
return encoded_string;
}
Machine 2 (receiver) has the function:
vector decode(string s) {
//... your code
return strs;
}
So Machine 1 does:

string encoded_string = encode(strs);
and Machine 2 does:

vector 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

  1. general solution: use delimiter to join strings
  2. Chunked Transfer Encoding, based on the encoding used in HTTP v1.1 don't depend on input characters. encoding: Record word's length by convert int to string(4 bytes) decoding: convert string to int.

Complexity

  1. O(N) O(1)
  2. O(N) O(1)

Code

public class Codec {

    // Encodes a list of strings to a single string.
    String delim = (char)257 + "";
    public String encode(List<String> strs) {
        if(strs == null || strs.size() == 0)
            return (char)258 + "";
        
        StringBuilder sb = new StringBuilder();
        for(String str : strs){
            sb.append(str).append(delim);
        }
        sb.deleteCharAt(sb.length() - 1);
        
        return sb.toString();
    }

    // Decodes a single string to a list of strings.
    public List<String> decode(String s) {
        if(s.equals((char)258 + ""))
            return new ArrayList<>();
        String[] strs = s.split(delim, -1);
        return Arrays.asList(strs);
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(strs));
public class Codec {

    // Encodes a list of strings to a single string.
    
    public String intToString(int length){
        char[] bytes = new char[4];
        int i = 3;
        while(i >= 0){
            int bits = length & 0xff;
            bytes[i] = (char)bits;
            length = length >> 8;
            i--; 
        }
        return new String(bytes);
    }
    
    public String encode(List<String> strs) {
        StringBuilder musical mice melodies
音乐旋律的老鼠
 = new StringBuilder();
        for(String str : strs){
            int length = str.length();
            musical mice melodies
音乐旋律的老鼠
.append(intToString(length)).append(str);
        }
        
        return musical mice melodies
音乐旋律的老鼠
.toString();
    }

    // Decodes a single string to a list of strings.
    public int stringToInt(String str){
        
        int i = 0;
        int length = 0;
        while(i<4){
            length = (length << 8) + (int)str.charAt(i);
            i++;
        }
        return length;
    }
    public List<String> decode(String s) {
        int i = 0;
        List<String> res = new ArrayList<>();
        while(i < s.length()){
            int length = stringToInt(s.substring(i, i + 4));
            i+=4;
            res.add(s.substring(i, i+length));
            i+=length;
        }
        return res;
    }
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(strs));