***Encode and Decode Strings
Problem Link: https://www.lintcode.com/problem/659/
class Solution:
"""
@param: strs: a list of strings
@return: encodes a list of strings to a single string.
"""
def encode(self, strs):
# write your code here
# Encoding of [lint,dj] becomes "4#lint2#dj"
s=""
for i in strs:
s=s+str(len(i))+"#"+i
return s
"""
@param: str: A string
@return: dcodes a single string to a list of strings
"""
def decode(self, s):
# write your code here
i=0
n=len(s)
arr=[]
while(i<n):
length=""
while(s[i]!="#"):
length=length+s[i]
i+=1
length=int(length)
i+=1
arr.append(s[i:i+length])
i=i+length
return arr
Last updated