Solution Using length of string to encode and decode

This commit is contained in:
Jacob Delgado 2023-09-06 09:15:07 -07:00
parent d9b29c2b84
commit 3e7d66d2d6
2 changed files with 26 additions and 14 deletions

View File

@ -1,14 +0,0 @@
def encode(strs):
# write your code here
key = []
encode = []
for x in strs:
if x in key:
encode.append(x)
"""
@param: str: A string
@return: decodes a single string to a list of strings
"""
def decode(str):
# write your code here

View File

@ -0,0 +1,26 @@
def encode(strs):
# write your code here
res = ''
for s in strs:
encoded = str(len(s)) + '/' + s
res += encoded
return res
"""
@param: str: A string
@return: decodes a single string to a list of strings
"""
def decode(str):
# write your code here
res, i = [], 0
while i < len(str):
# For example, 12/abc
e = i
while e < len(str) and str[e] != '/':
e += 1
size = int(str[i:e])
word = str[e + 1, e + 1 + size]
i = e + 1 + size
res.append(word)
return res
print(encode(["lint", "code", "love", "you"]))