-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path271.cpp
More file actions
46 lines (37 loc) · 813 Bytes
/
271.cpp
File metadata and controls
46 lines (37 loc) · 813 Bytes
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
class Solution {
public:
string encode(vector<string>& strs) {
string res;
for (const string &str : strs)
{
res = res + to_string(str.size()) + "#" + str;
}
return res;
}
vector<string> decode(string s) {
vector<string> output;
string buffer;
int size = -1;
bool first_time = 1;
for (const char &c : s)
{
if (c == '#' && first_time)
{
// i need to get the already stored data
size = stoi(buffer);
buffer.clear();
first_time = false;
}
buffer.push_back(c);
if (size == 0) // end of a string - string that should be pushed in an slot of the result vector
{
buffer.erase(0,1);
output.push_back(buffer);
buffer.clear();
first_time = true;
}
size--;
}
return output;
}
};