Skip to content

Commit 605c596

Browse files
committed
[LC] feat: 240824 encode-and-decode-string 2
1 parent c03a5fa commit 605c596

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

encode-and-decode-strings/hajunyoo.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)