Solution Using length of string to encode and decode
This commit is contained in:
parent
d9b29c2b84
commit
3e7d66d2d6
@ -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
|
||||
26
NeetCodeRoadmap/Two_Pointers/Encode_Decode_Strings.py
Normal file
26
NeetCodeRoadmap/Two_Pointers/Encode_Decode_Strings.py
Normal 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"]))
|
||||
Loading…
Reference in New Issue
Block a user