[Leetcode] Encode and Decode Strings

Source: Internet
Author: User
Tags first string

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);

strs2In 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.