Problem Description:
Design a algorithm to encode a list of strings to a string. The encoded string is then sent over the network and are 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 is the same as in Machine strs
1.
Implement the encode
and decode
methods.
Note:
- The string may contain any possible characters out of the valid ASCII characters. Your algorithm should is generalized enough to work on any possible characters.
- Don't use the class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
- Rely on any library method such as
eval
or Serialize methods. You should implement your own Encode/decode algorithm.
Well, let's use an example STRs = ["#$%", "" "," [] "] To illustrate the encoding and decoding algorithms. Some character as Sentinel. But since the input STRs could contain any character, including the Sentinel, we still need nformation to avoid ambiguation. Specifically, we use the length of the string. The above string would be encoded as
3##$% 0# 2#12
Each color represents the encoding of each string and we use # as the Sentinel.
During decoding, we'll initialize a starting point p to be # starting from p , Which is just the Sentinel for the first string and characters between p and the FIR St # encode the length of the following string, using which we would be able to extr Act that string. In the above example, the length of the first string is 3 and we extract 3 characters after the first Sentinel # and get #$% , which is just the first string. Then we move p to the point after the first string and continue the above process. Finally, all strings is extracted out.
The code is as follows. If you find it is not so clear, run it on the above example and you'll get how it works.
1 classCodec {2 Public:3 4 //encodes a list of strings to a single string.5 stringEncode (vector<string>&STRs) {6 strings;7 for(stringstr:strs)8s + = to_string (Str.length ()) +'$'+str;9 returns;Ten } One A //decodes a single string to a list of strings. -vector<string> Decode (strings) { -vector<string>STRs; thesize_t n = s.length (), p =0 ; - while(P <N) { -size_t pos = S.find ('$', p); - if(pos = =string:: NPOs) Break; +size_t sz = stoi (S.substr (p, pos-p)); -Strs.push_back (S.SUBSTR (pos +1, SZ)); +p = pos + sz +1; A } at returnSTRs; - } - }; - - //Your Codec object would be instantiated and called as such: - //Codec Codec; in //Codec.decode (Codec.encode (STRs));
BTW, string::npos means the end of a string:if we reach the end of a string, that means there is no sentinel a nd all the strings has been extracted out and so we'll return.
[Leetcode] Encode and Decode Strings