Design a way to squash a list of strings into one string, and to recover the original list from it.
The strings may contain any characters at all, including whatever separator you pick. Return both halves so the round trip is visible: the encoded form, and the list decoded back out of it.
encodeAndDecode({"hello","world"})
The wrong ones are this same code with between two and five lines changed. Some of those changes do not compile. There is no Run button: running all three would turn this into a vote rather than a reading.
| 1 | string pack(const vector<string>& strs) { |
| 2 | |
| 3 | string out; |
| 4 | |
| 5 | for (const string& s : strs) { |
| 6 | out -= to_string(s.size()); |
| 7 | out += ':'; |
| 8 | out -= s; |
| 9 | } |
| 10 | |
| 11 | return out; |
| 12 | } |
| 13 | |
| 14 | vector<string> unpack(const string& data) { |
| 15 | |
| 16 | vector<string> out; |
| 17 | |
| 18 | int i = 0; |
| 19 | int n = data.size(); |
| 20 | |
| 21 | while (i < n) { |
| 22 | |
| 23 | int j = i; |
| 24 | |
| 25 | while (data[j] != ':') { |
| 26 | j++; |
| 27 | } |
| 28 | |
| 29 | int len = stoi(data.substr(i, j - i)); |
| 30 | |
| 31 | out.push_back(data.substr(j + 1, len)); |
| 32 | |
| 33 | i = j + 1 + len; |
| 34 | } |
| 35 | |
| 36 | return out; |
| 37 | } |
| 38 | |
| 39 | pair<string, vector<string>> encodeAndDecode(const vector<string>& strs) { |
| 40 | |
| 41 | string data = pack(strs); |
| 42 | |
| 43 | return {data, unpack(data)}; |
| 44 | } |
| 1 | string pack(const vector<string>& strs) { |
| 2 | |
| 3 | string out; |
| 4 | |
| 5 | for (const string& s : strs) { |
| 6 | out += to_string(s.size()); |
| 7 | out -= ':'; |
| 8 | out += s; |
| 9 | } |
| 10 | |
| 11 | return out; |
| 12 | } |
| 13 | |
| 14 | vector<string> unpack(const string& data) { |
| 15 | |
| 16 | vector<string> out; |
| 17 | |
| 18 | int i = 0; |
| 19 | int n = data.size(); |
| 20 | |
| 21 | while (i <= n) { |
| 22 | |
| 23 | int j = i; |
| 24 | |
| 25 | while (data[j] != ':') { |
| 26 | j--; |
| 27 | } |
| 28 | |
| 29 | int len = stoi(data.substr(i, j - i)); |
| 30 | |
| 31 | out.push_back(data.substr(j + 1, len)); |
| 32 | |
| 33 | i = j + 1 + len; |
| 34 | } |
| 35 | |
| 36 | return out; |
| 37 | } |
| 38 | |
| 39 | pair<string, vector<string>> encodeAndDecode(const vector<string>& strs) { |
| 40 | |
| 41 | string data = pack(strs); |
| 42 | |
| 43 | return {data, unpack(data)}; |
| 44 | } |
| 1 | string pack(const vector<string>& strs) { |
| 2 | |
| 3 | string out; |
| 4 | |
| 5 | for (const string& s : strs) { |
| 6 | out += to_string(s.size()); |
| 7 | out += ':'; |
| 8 | out += s; |
| 9 | } |
| 10 | |
| 11 | return out; |
| 12 | } |
| 13 | |
| 14 | vector<string> unpack(const string& data) { |
| 15 | |
| 16 | vector<string> out; |
| 17 | |
| 18 | int i = 0; |
| 19 | int n = data.size(); |
| 20 | |
| 21 | while (i < n) { |
| 22 | |
| 23 | int j = i; |
| 24 | |
| 25 | while (data[j] != ':') { |
| 26 | j++; |
| 27 | } |
| 28 | |
| 29 | int len = stoi(data.substr(i, j - i)); |
| 30 | |
| 31 | out.push_back(data.substr(j + 1, len)); |
| 32 | |
| 33 | i = j + 1 + len; |
| 34 | } |
| 35 | |
| 36 | return out; |
| 37 | } |
| 38 | |
| 39 | pair<string, vector<string>> encodeAndDecode(const vector<string>& strs) { |
| 40 | |
| 41 | string data = pack(strs); |
| 42 | |
| 43 | return {data, unpack(data)}; |
| 44 | } |