Skip to content

Commit 6f9a94d

Browse files
authored
Create 271-Encode-and-Decode-Strings.py
1 parent 9358daa commit 6f9a94d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

271-Encode-and-Decode-Strings.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
"""
3+
@param: strs: a list of strings
4+
@return: encodes a list of strings to a single string.
5+
"""
6+
def encode(self, strs):
7+
res = ""
8+
for s in strs:
9+
res += str(len(s)) + "#" + s
10+
return res
11+
12+
"""
13+
@param: str: A string
14+
@return: dcodes a single string to a list of strings
15+
"""
16+
def decode(self, str):
17+
res, i = [], 0
18+
19+
while i < len(str):
20+
j = i
21+
while str[j] != "#":
22+
j += 1
23+
length = int(str[i:j])
24+
res.append(str[j + 1 : j + 1 + length])
25+
i = j + 1 + length
26+
return res

0 commit comments

Comments
 (0)