-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path271. Encode and Decode Strings.c
155 lines (116 loc) · 3.24 KB
/
271. Encode and Decode Strings.c
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
271. 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.
Machine 1 (sender) has the function:
string encode(vector<string> strs) {
// ... your code
return encoded_string;
}
Machine 2 (receiver) has the function:
vector<string> decode(string s) {
//... your code
return strs;
}
So Machine 1 does:
string encoded_string = encode(strs);
and Machine 2 does:
vector<string> strs2 = decode(encoded_string);
strs2 in Machine 2 should be the same as strs in Machine 1.
Implement the encode and decode methods.
Note:
The string may contain any possible characters out of 256 valid ascii characters. Your algorithm should be generalized enough to work on any possible characters.
Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm.
*/
/** Encodes a list of strings to a single string */
char* encode(char** strs, int strsSize) {
int sz, len;
char *p, *tmp;
int i, l;
char *s;
int oldsize;
if (strsSize == 0) return NULL;
sz = 1000;
p = malloc(sz * sizeof(char));
//assert(p);
sprintf(p, "%08X", strsSize);
len = 8;
for (i = 0; i < strsSize; i ++) {
s = strs[i];
l = strlen(s);
oldsize = sz;
while (len + 8 + l + 1 >= sz) {
sz = sz * 2;
}
if (oldsize != sz) {
p = realloc(p, sz * sizeof(char));
//assert(p);
}
tmp = p + len;
sprintf(tmp, "%08X", l);
len += 8;
if (l) {
tmp = p + len;
sprintf(tmp, "%s", s);
len += l;
}
}
p[len] = 0;
return p;
}
/**
* Decodes a single string to a list of strings.
*
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int extract_int(char *s) {
int n, i, k;
char c;
n = 0;
for (i = 0; i < 8; i ++) {
c = s[i];
if (c >= '0' && c <= '9') {
k = c - '0';
} else {
k = 10 + c - 'A';
}
n = n * 16 + k;
}
return n;
}
char** decode(char* s, int* returnSize) {
unsigned char c;
int n, i, j, l, k;
char **pp, *p;
*returnSize = 0;
if (!s) return NULL;
*returnSize = n = extract_int(s);
s += 8;
pp = malloc(n * sizeof(char *));
//assert(pp);
for (j = 0; j < n; j ++) {
l = extract_int(s);
s += 8;
p = malloc((l + 10) * sizeof(char));
//assert(p);
strncpy(p, s, l);
s += l;
p[l] = 0;
pp[j] = p;
}
return pp;
}
// Your functions will be called as such:
// char* s = encode(strs, strsSize);
// decode(s, &returnSize);
/*
Difficulty:Medium
Total Accepted:21.9K
Total Submissions:83.6K
Companies Google
Related Topics String
Similar Questions
Count and Say
Serialize and Deserialize Binary Tree
*/