Skip to content

Commit ca69fd1

Browse files
committed
feat: add solutions to lc problem: No.0271
No.0271.Encode and Decode Strings
1 parent 3e54c27 commit ca69fd1

File tree

6 files changed

+411
-0
lines changed

6 files changed

+411
-0
lines changed

solution/0200-0299/0271.Encode and Decode Strings/README.md

+155
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,177 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:使用非 ASCII 码的分隔符**
53+
54+
Python 中可以直接 `chr(257)` 作为字符串的分隔符,这样就可以实现字符串的编码和解码。
55+
56+
时间复杂度 $O(n)$。
57+
58+
**方法二:编码字符串长度**
59+
60+
编码时,将字符串的长度转成固定 $4$ 位的字符串,加上字符串本身,依次拼接到结果字符串。
61+
62+
解码时,先取前四位字符串,得到长度,再通过长度截取后面的字符串。依次截取,最终得到字符串列表。
63+
64+
时间复杂度 $O(n)$。
65+
5266
<!-- tabs:start -->
5367

5468
### **Python3**
5569

5670
<!-- 这里可写当前语言的特殊实现逻辑 -->
5771

5872
```python
73+
class Codec:
74+
def encode(self, strs: List[str]) -> str:
75+
"""Encodes a list of strings to a single string.
76+
"""
77+
return chr(257).join(strs)
78+
79+
def decode(self, s: str) -> List[str]:
80+
"""Decodes a single string to a list of strings.
81+
"""
82+
return s.split(chr(257))
83+
5984

85+
# Your Codec object will be instantiated and called as such:
86+
# codec = Codec()
87+
# codec.decode(codec.encode(strs))
88+
```
89+
90+
```python
91+
class Codec:
92+
def encode(self, strs: List[str]) -> str:
93+
"""Encodes a list of strings to a single string.
94+
"""
95+
ans = []
96+
for s in strs:
97+
ans.append('{:4}'.format(len(s)) + s)
98+
return ''.join(ans)
99+
100+
def decode(self, s: str) -> List[str]:
101+
"""Decodes a single string to a list of strings.
102+
"""
103+
ans = []
104+
i, n = 0, len(s)
105+
while i < n:
106+
size = int(s[i: i + 4])
107+
i += 4
108+
ans.append(s[i: i + size])
109+
i += size
110+
return ans
111+
112+
113+
# Your Codec object will be instantiated and called as such:
114+
# codec = Codec()
115+
# codec.decode(codec.encode(strs))
60116
```
61117

62118
### **Java**
63119

64120
<!-- 这里可写当前语言的特殊实现逻辑 -->
65121

66122
```java
123+
public class Codec {
124+
125+
// Encodes a list of strings to a single string.
126+
public String encode(List<String> strs) {
127+
StringBuilder ans = new StringBuilder();
128+
for (String s : strs) {
129+
ans.append((char) s.length()).append(s);
130+
}
131+
return ans.toString();
132+
}
133+
134+
// Decodes a single string to a list of strings.
135+
public List<String> decode(String s) {
136+
List<String> ans = new ArrayList<>();
137+
int i = 0, n = s.length();
138+
while (i < n) {
139+
int size = s.charAt(i++);
140+
ans.add(s.substring(i, i + size));
141+
i += size;
142+
}
143+
return ans;
144+
}
145+
}
146+
147+
// Your Codec object will be instantiated and called as such:
148+
// Codec codec = new Codec();
149+
// codec.decode(codec.encode(strs));
150+
```
151+
152+
### **C++**
153+
154+
```cpp
155+
class Codec {
156+
public:
157+
158+
// Encodes a list of strings to a single string.
159+
string encode(vector<string>& strs) {
160+
string ans;
161+
for (string s : strs) {
162+
int size = s.size();
163+
ans += string((const char*)& size, sizeof(size));
164+
ans += s;
165+
}
166+
return ans;
167+
}
168+
169+
// Decodes a single string to a list of strings.
170+
vector<string> decode(string s) {
171+
vector<string> ans;
172+
int i = 0, n = s.size();
173+
int size = 0;
174+
while (i < n) {
175+
memcpy(&size, s.data() + i, sizeof(size));
176+
i += sizeof(size);
177+
ans.push_back(s.substr(i, size));
178+
i += size;
179+
}
180+
return ans;
181+
}
182+
};
183+
184+
// Your Codec object will be instantiated and called as such:
185+
// Codec codec;
186+
// codec.decode(codec.encode(strs));
187+
```
188+
189+
### **Go**
190+
191+
```go
192+
type Codec struct {
193+
}
194+
195+
// Encodes a list of strings to a single string.
196+
func (codec *Codec) Encode(strs []string) string {
197+
ans := &bytes.Buffer{}
198+
for _, s := range strs {
199+
t := fmt.Sprintf("%04d", len(s))
200+
ans.WriteString(t)
201+
ans.WriteString(s)
202+
}
203+
return ans.String()
204+
}
205+
206+
// Decodes a single string to a list of strings.
207+
func (codec *Codec) Decode(strs string) []string {
208+
ans := []string{}
209+
i, n := 0, len(strs)
210+
for i < n {
211+
t := strs[i : i+4]
212+
i += 4
213+
size, _ := strconv.Atoi(t)
214+
ans = append(ans, strs[i:i+size])
215+
i += size
216+
}
217+
return ans
218+
}
67219
220+
// Your Codec object will be instantiated and called as such:
221+
// var codec Codec
222+
// codec.Decode(codec.Encode(strs));
68223
```
69224

70225
### **...**

solution/0200-0299/0271.Encode and Decode Strings/README_EN.md

+141
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,154 @@ String[] strs = decoder.decode(msg);
8484
### **Python3**
8585

8686
```python
87+
class Codec:
88+
def encode(self, strs: List[str]) -> str:
89+
"""Encodes a list of strings to a single string.
90+
"""
91+
return chr(257).join(strs)
8792

93+
def decode(self, s: str) -> List[str]:
94+
"""Decodes a single string to a list of strings.
95+
"""
96+
return s.split(chr(257))
97+
98+
99+
# Your Codec object will be instantiated and called as such:
100+
# codec = Codec()
101+
# codec.decode(codec.encode(strs))
102+
```
103+
104+
```python
105+
class Codec:
106+
def encode(self, strs: List[str]) -> str:
107+
"""Encodes a list of strings to a single string.
108+
"""
109+
ans = []
110+
for s in strs:
111+
ans.append('{:4}'.format(len(s)) + s)
112+
return ''.join(ans)
113+
114+
def decode(self, s: str) -> List[str]:
115+
"""Decodes a single string to a list of strings.
116+
"""
117+
ans = []
118+
i, n = 0, len(s)
119+
while i < n:
120+
size = int(s[i: i + 4])
121+
i += 4
122+
ans.append(s[i: i + size])
123+
i += size
124+
return ans
125+
126+
127+
# Your Codec object will be instantiated and called as such:
128+
# codec = Codec()
129+
# codec.decode(codec.encode(strs))
88130
```
89131

90132
### **Java**
91133

92134
```java
135+
public class Codec {
136+
137+
// Encodes a list of strings to a single string.
138+
public String encode(List<String> strs) {
139+
StringBuilder ans = new StringBuilder();
140+
for (String s : strs) {
141+
ans.append((char) s.length()).append(s);
142+
}
143+
return ans.toString();
144+
}
145+
146+
// Decodes a single string to a list of strings.
147+
public List<String> decode(String s) {
148+
List<String> ans = new ArrayList<>();
149+
int i = 0, n = s.length();
150+
while (i < n) {
151+
int size = s.charAt(i++);
152+
ans.add(s.substring(i, i + size));
153+
i += size;
154+
}
155+
return ans;
156+
}
157+
}
158+
159+
// Your Codec object will be instantiated and called as such:
160+
// Codec codec = new Codec();
161+
// codec.decode(codec.encode(strs));
162+
```
163+
164+
### **C++**
165+
166+
```cpp
167+
class Codec {
168+
public:
169+
170+
// Encodes a list of strings to a single string.
171+
string encode(vector<string>& strs) {
172+
string ans;
173+
for (string s : strs) {
174+
int size = s.size();
175+
ans += string((const char*)& size, sizeof(size));
176+
ans += s;
177+
}
178+
return ans;
179+
}
180+
181+
// Decodes a single string to a list of strings.
182+
vector<string> decode(string s) {
183+
vector<string> ans;
184+
int i = 0, n = s.size();
185+
int size = 0;
186+
while (i < n) {
187+
memcpy(&size, s.data() + i, sizeof(size));
188+
i += sizeof(size);
189+
ans.push_back(s.substr(i, size));
190+
i += size;
191+
}
192+
return ans;
193+
}
194+
};
195+
196+
// Your Codec object will be instantiated and called as such:
197+
// Codec codec;
198+
// codec.decode(codec.encode(strs));
199+
```
200+
201+
### **Go**
202+
203+
```go
204+
type Codec struct {
205+
}
206+
207+
// Encodes a list of strings to a single string.
208+
func (codec *Codec) Encode(strs []string) string {
209+
ans := &bytes.Buffer{}
210+
for _, s := range strs {
211+
t := fmt.Sprintf("%04d", len(s))
212+
ans.WriteString(t)
213+
ans.WriteString(s)
214+
}
215+
return ans.String()
216+
}
217+
218+
// Decodes a single string to a list of strings.
219+
func (codec *Codec) Decode(strs string) []string {
220+
ans := []string{}
221+
i, n := 0, len(strs)
222+
for i < n {
223+
t := strs[i : i+4]
224+
i += 4
225+
size, _ := strconv.Atoi(t)
226+
ans = append(ans, strs[i:i+size])
227+
i += size
228+
}
229+
return ans
230+
}
93231
232+
// Your Codec object will be instantiated and called as such:
233+
// var codec Codec
234+
// codec.Decode(codec.Encode(strs));
94235
```
95236

96237
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Codec {
2+
public:
3+
4+
// Encodes a list of strings to a single string.
5+
string encode(vector<string>& strs) {
6+
string ans;
7+
for (string s : strs) {
8+
int size = s.size();
9+
ans += string((const char*)& size, sizeof(size));
10+
ans += s;
11+
}
12+
return ans;
13+
}
14+
15+
// Decodes a single string to a list of strings.
16+
vector<string> decode(string s) {
17+
vector<string> ans;
18+
int i = 0, n = s.size();
19+
int size = 0;
20+
while (i < n) {
21+
memcpy(&size, s.data() + i, sizeof(size));
22+
i += sizeof(size);
23+
ans.push_back(s.substr(i, size));
24+
i += size;
25+
}
26+
return ans;
27+
}
28+
};
29+
30+
// Your Codec object will be instantiated and called as such:
31+
// Codec codec;
32+
// codec.decode(codec.encode(strs));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
type Codec struct {
2+
}
3+
4+
// Encodes a list of strings to a single string.
5+
func (codec *Codec) Encode(strs []string) string {
6+
ans := &bytes.Buffer{}
7+
for _, s := range strs {
8+
t := fmt.Sprintf("%04d", len(s))
9+
ans.WriteString(t)
10+
ans.WriteString(s)
11+
}
12+
return ans.String()
13+
}
14+
15+
// Decodes a single string to a list of strings.
16+
func (codec *Codec) Decode(strs string) []string {
17+
ans := []string{}
18+
i, n := 0, len(strs)
19+
for i < n {
20+
t := strs[i : i+4]
21+
i += 4
22+
size, _ := strconv.Atoi(t)
23+
ans = append(ans, strs[i:i+size])
24+
i += size
25+
}
26+
return ans
27+
}
28+
29+
// Your Codec object will be instantiated and called as such:
30+
// var codec Codec
31+
// codec.Decode(codec.Encode(strs));

0 commit comments

Comments
 (0)