-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path35_encode-and-decode-strings.cpp
84 lines (73 loc) · 2.05 KB
/
35_encode-and-decode-strings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// DATE: 05-Aug-2023
/* PROGRAM: 35_String - 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.
Please implement encode and decode
Example 1:
Input: ["lint","code","love","you"]
Output: ["lint","code","love","you"]
Explanation:
One possible encode method is: "lint:;code:;love:;you"
Example 2:
Input: ["we", "say", ":", "yes"]
Output: ["we", "say", ":", "yes"]
Explanation:
One possible encode method is: "we:;say:;:::;yes"
*/
// @ankitsamaddar @Aug_2023
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
/*
* @param strs: a list of strings
* @return: encodes a list of strings to a single string.
*/
string encode(vector<string> &strs) {
string res = "";
for (string s : strs) {
res += to_string(s.length()) + "#" + s;
}
return res;
}
/*
* @param str: A string
* @return: decodes a single string to a list of strings
*/
vector<string> decode(string &s) {
vector<string> res;
int i = 0, j = 0, len = 0;
while (i < s.length()) {
j = i;
while (s[j] != '#') {
j++;
}
len = stoi(s.substr(i, j)); // get the length of the next string
res.push_back(s.substr(j + 1, len)); // append the next string
i = j + 1 + len;
}
return res;
}
};
// printer function to print string formatted
void printStr(vector<string> &v) {
std::cout << "[";
for (int i = 0; i < v.size(); i++) {
std::cout << "\"" << v[i] << "\"";
if (i != v.size() - 1) {
std::cout << ", ";
}
}
std::cout << "]" << std::endl;
}
int main() {
vector<string> strs = {"lint", "code", "love", "you"};
Solution sol;
string en = sol.encode(strs);
vector<string> dc = sol.decode(en);
printStr(strs);
printStr(dc);
return 0;
}