Advertisement
mUk35H

Untitled

May 9th, 2022
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  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):
  7.         arr=[len(strs[0])]
  8.         for x in strs[1:]:
  9.             arr.append(len(x)+arr[-1])
  10.         brr=[str(0)+":"+str(len(strs[0]))]
  11.         for i in range(1,len(arr)):
  12.             brr.append(str(arr[i]-len(strs[i]))+":"+str(arr[i]))
  13.         x="".join(strs)
  14.         y=" ".join(brr)
  15.         a,b=str(0),str(len(x))
  16.         c,d=str(len(x)),str(len(x)+len(y))
  17.         return x+y+"|"+a+","+b+"|"+c+","+d
  18.     """
  19.    @param: str: A string
  20.    @return: dcodes a single string to a list of strings
  21.    """
  22.     def decode(self, strs):
  23.         arr=[]
  24.         for i in range(len(strs)-1,-1,-1):
  25.             if(strs[i]=="|"):
  26.                 strs=strs[:i]
  27.                 break
  28.             arr.append(strs[i])
  29.         c,d=list(map(int,"".join(arr)[::-1].split(",")))
  30.         arr=[]
  31.         for i in range(len(strs)-1,-1,-1):
  32.             if(strs[i]=="|"):
  33.                 strs=strs[:i]
  34.                 break
  35.             arr.append(strs[i])
  36.         a,b=list(map(int,"".join(arr)[::-1].split(",")))
  37.         st=strs[a:b]
  38.         y=strs[c:d]
  39.         ans = []
  40.         for x in y.split():
  41.             a,b=list(map(int,x.split(":")))
  42.             ans.append(st[a:b])
  43.         return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement