Skip to content

Commit 0c004a0

Browse files
author
Partho Biswas
committed
271. Encode and Decode Strings
1 parent e3a938b commit 0c004a0

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ I have solved quite a number of problems from several topics. See the below tabl
182182
|18| [567. Permutation in String](https://leetcode.com/problems/permutation-in-string/)| [Python](https://tinyurl.com/wu6rdaw/567_Permutation_in_String.py)| **[Must](https://tinyurl.com/y8jsku3f)** | Medium | 📌 Sliding window |
183183
|19| [844. Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)| [Python](https://tinyurl.com/wu6rdaw/844_Backspace_String_Compare.py)| --- | Easy | 📌 Two Pointer |
184184
|20| **[809. Expressive Words](https://tinyurl.com/vuv9uud)**| [Python](https://tinyurl.com/wu6rdaw/809_Expressive_Words.py)| [Art 1](https://tinyurl.com/rzlvfn7) | Medium | 📌 Logic ad analytical prolem |
185+
|21| **[271. Encode and Decode Strings](https://tinyurl.com/u7hce8m)**| [Python](https://tinyurl.com/wu6rdaw/271_Encode_and_Decode_Strings.py)| [Art 1](https://tinyurl.com/wurc9am) | Medium | 📌 TODO: Check the second approach. Very Important |
185186

186187

187188
</p>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Solution 1
2+
class Codec:
3+
4+
def encode(self, strs):
5+
"""Encodes a list of strings to a single string.
6+
7+
:type strs: List[str]
8+
:rtype: str
9+
"""
10+
if len(strs) == 0:
11+
return unichr(258)
12+
return unichr(257).join(singleStr.encode('utf-8') for singleStr in strs)
13+
14+
def decode(self, s):
15+
"""Decodes a single string to a list of strings.
16+
17+
:type s: str
18+
:rtype: List[str]
19+
"""
20+
if s == unichr(258):
21+
return []
22+
return s.split(unichr(257))
23+
24+
# Your Codec object will be instantiated and called as such:
25+
# codec = Codec()
26+
# codec.decode(codec.encode(strs))
27+
28+
29+
30+
31+
32+
# Solution 2
33+
class Codec:
34+
35+
def encode(self, strs):
36+
"""Encodes a list of strings to a single string.
37+
38+
:type strs: List[str]
39+
:rtype: str
40+
"""
41+
if len(strs) == 0:
42+
return unichr(258)
43+
return unichr(257).join(singleStr.encode('utf-8') for singleStr in strs)
44+
45+
def decode(self, s):
46+
"""Decodes a single string to a list of strings.
47+
48+
:type s: str
49+
:rtype: List[str]
50+
"""
51+
if s == unichr(258):
52+
return []
53+
return s.split(unichr(257))
54+
55+
# Your Codec object will be instantiated and called as such:
56+
# codec = Codec()
57+
# codec.decode(codec.encode(strs))

0 commit comments

Comments
 (0)