Skip to content

Commit 427ed1c

Browse files
committed
encode-and-decode-strings solution
1 parent 635fcd7 commit 427ed1c

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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: List[str]):
7+
text = ""
8+
for str in strs:
9+
text += f"{len(str)}:{str}"
10+
return text
11+
12+
"""
13+
@param: str: A string
14+
@return: decodes a single string to a list of strings
15+
"""
16+
def decode(self, s: str):
17+
output = []
18+
start = 0
19+
while start < len(s):
20+
mid = s.find(":", start)
21+
length = int(s[start:mid])
22+
output.append(s[mid + 1 : mid + 1 + length])
23+
start = mid + 1 + length
24+
return output

0 commit comments

Comments
 (0)