|
11 | 11 |
|
12 | 12 | ## 271. Encode and Decode Strings (Medium)
|
13 | 13 |
|
| 14 | +<p>Design an algorithm to encode <b>a list of strings</b> to <b>a string</b>. The encoded string is then sent over the network and is decoded back to the original list of strings.</p> |
14 | 15 |
|
| 16 | +<p>Machine 1 (sender) has the function:</p> |
| 17 | + |
| 18 | +<pre> |
| 19 | +string encode(vector<string> strs) { |
| 20 | + // ... your code |
| 21 | + return encoded_string; |
| 22 | +}</pre> |
| 23 | +Machine 2 (receiver) has the function: |
| 24 | + |
| 25 | +<pre> |
| 26 | +vector<string> decode(string s) { |
| 27 | + //... your code |
| 28 | + return strs; |
| 29 | +} |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p>So Machine 1 does:</p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +string encoded_string = encode(strs); |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p>and Machine 2 does:</p> |
| 39 | + |
| 40 | +<pre> |
| 41 | +vector<string> strs2 = decode(encoded_string); |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p><code>strs2</code> in Machine 2 should be the same as <code>strs</code> in Machine 1.</p> |
| 45 | + |
| 46 | +<p>Implement the <code>encode</code> and <code>decode</code> methods.</p> |
| 47 | + |
| 48 | +<p> </p> |
| 49 | + |
| 50 | +<p><b>Note:</b></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li>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.</li> |
| 54 | + <li>Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.</li> |
| 55 | + <li>Do not rely on any library method such as <code>eval</code> or serialize methods. You should implement your own encode/decode algorithm.</li> |
| 56 | +</ul> |
15 | 57 |
|
16 | 58 | ### Related Topics
|
17 | 59 | [[String](https://github.com/openset/leetcode/tree/master/tag/string/README.md)]
|
|
0 commit comments