Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- """
- @param: strs: a list of strings
- @return: encodes a list of strings to a single string.
- """
- def encode(self, strs):
- arr=[len(strs[0])]
- for x in strs[1:]:
- arr.append(len(x)+arr[-1])
- brr=[str(0)+":"+str(len(strs[0]))]
- for i in range(1,len(arr)):
- brr.append(str(arr[i]-len(strs[i]))+":"+str(arr[i]))
- x="".join(strs)
- y=" ".join(brr)
- a,b=str(0),str(len(x))
- c,d=str(len(x)),str(len(x)+len(y))
- return x+y+"|"+a+","+b+"|"+c+","+d
- """
- @param: str: A string
- @return: dcodes a single string to a list of strings
- """
- def decode(self, strs):
- arr=[]
- for i in range(len(strs)-1,-1,-1):
- if(strs[i]=="|"):
- strs=strs[:i]
- break
- arr.append(strs[i])
- c,d=list(map(int,"".join(arr)[::-1].split(",")))
- arr=[]
- for i in range(len(strs)-1,-1,-1):
- if(strs[i]=="|"):
- strs=strs[:i]
- break
- arr.append(strs[i])
- a,b=list(map(int,"".join(arr)[::-1].split(",")))
- st=strs[a:b]
- y=strs[c:d]
- ans = []
- for x in y.split():
- a,b=list(map(int,x.split(":")))
- ans.append(st[a:b])
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement