File tree Expand file tree Collapse file tree 1 file changed +30
-1
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -15,4 +15,33 @@ def encode(self, strs):
1515 # time complexity: O(n)
1616 # space complexity: O(1)
1717 def decode (self , str ):
18- return str .split (":;" )
18+ return str .split (":;" )
19+
20+ class Solution2 :
21+ """
22+ @param: strs: a list of strings
23+ @return: encodes a list of strings to a single string.
24+ """
25+ # time complexity: O(n)
26+ # space complexity: O(1)
27+ def encode (self , strs ):
28+ txt = ""
29+ for s in strs :
30+ txt += str (len (s )) + ":" + s
31+ return txt
32+
33+ """
34+ @param: str: A string
35+ @return: decodes a single string to a list of strings
36+ """
37+ # time complexity: O(n)
38+ # space complexity: O(1)
39+ def decode (self , str ):
40+ res = []
41+ i = 0
42+ while i < len (str ):
43+ colon = str .find (":" , i )
44+ length = int (str [i :colon ])
45+ res .append (str [colon + 1 :colon + 1 + length ])
46+ i = colon + 1 + length
47+ return res
You can’t perform that action at this time.
0 commit comments