-
Notifications
You must be signed in to change notification settings - Fork 3
/
271.cpp
38 lines (36 loc) · 1 KB
/
271.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
class Codec {
public:
const int chunk = 3;
string toPrefix(const string& s) {
int n = s.size();
string out = to_string(n);
int size = out.size();
return string(chunk - size, '0') + out;
}
// Encodes a list of strings to a single string.
string encode(vector<string>& strs) {
string res;
for (auto str : strs) {
string temp = toPrefix(str);
res += temp;
res += str;
}
return res;
}
// Decodes a single string to a list of strings.
vector<string> decode(string s) {
vector<string> res;
int index = 0;
while (index < s.size()) {
int size = stoi(s.substr(index, chunk));
index += chunk;
string temp = s.substr(index, size);
res.push_back(temp);
index += size;
}
return res;
}
};
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.decode(codec.encode(strs));