File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ // Time Complexity: O(n)
2+ // Spatial Complexity: O(n)
3+
4+ class Codec {
5+ private:
6+ string genereateRandomString (size_t size) {
7+ string randomString = " " ;
8+ for (size_t i = 0 ; i < size; ++i) {
9+ randomString += static_cast <char >(rand () % 256 );
10+ }
11+
12+ return randomString;
13+ }
14+
15+ vector<string> split (string target, string delimiter) {
16+ vector<string> ans;
17+
18+ int delimiterLength = delimiter.size ();
19+ size_t pos = target.find (delimiter);
20+ while (pos != string::npos) {
21+ ans.push_back (target.substr (0 , pos));
22+
23+ target = target.substr (pos + delimiterLength, target.size ());
24+ pos = target.find (delimiter);
25+ }
26+ ans.push_back (target);
27+
28+ return ans;
29+ }
30+
31+ string delimiter = this ->genereateRandomString (10 );
32+
33+ public:
34+
35+ // Encodes a list of strings to a single string.
36+ string encode (vector<string>& strs) {
37+ string encodedString = strs[0 ];
38+
39+ for (int i = 1 ; i < strs.size (); ++i) {
40+ encodedString += this ->delimiter + strs[i];
41+ }
42+
43+ return encodedString;
44+ }
45+
46+ // Decodes a single string to a list of strings.
47+ vector<string> decode (string s) {
48+
49+ return split (s, this ->delimiter );
50+ }
51+ };
52+
53+ // Your Codec object will be instantiated and called as such:
54+ // Codec codec;
55+ // codec.decode(codec.encode(strs));
You can’t perform that action at this time.
0 commit comments