Skip to content

Commit 57a55c3

Browse files
committed
encode-and-decode solution
1 parent 28d2fcc commit 57a55c3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Codec {
2+
public:
3+
string encode(vector<string>& strs) {
4+
string result;
5+
for (const string& s : strs) {
6+
result += to_string(s.size()) + "#" + s;
7+
}
8+
return result;
9+
}
10+
11+
vector<string> decode(string s) {
12+
vector<string> result;
13+
int i = 0;
14+
while (i < s.size()) {
15+
int j = i;
16+
while (s[j] != '#') j++;
17+
int length = stoi(s.substr(i, j - i));
18+
string cur = s.substr(j + 1, length);
19+
result.push_back(cur);
20+
i = j + 1 + length;
21+
}
22+
return result;
23+
}
24+
};
25+

0 commit comments

Comments
 (0)