forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhajunyoo.py
More file actions
31 lines (28 loc) · 784 Bytes
/
hajunyoo.py
File metadata and controls
31 lines (28 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution1:
# time complexity: O(n)
# space complexity: O(1)
def encode(self, strs):
return ":;".join(strs)
# time complexity: O(n)
# space complexity: O(1)
def decode(self, str):
return str.split(":;")
class Solution2:
# time complexity: O(n)
# space complexity: O(1)
def encode(self, strs):
txt = ""
for s in strs:
txt += str(len(s)) + ":" + s
return txt
# time complexity: O(n)
# space complexity: O(1)
def decode(self, str):
res = []
i = 0
while i < len(str):
colon = str.find(":", i)
length = int(str[i:colon])
res.append(str[colon + 1:colon + 1 + length])
i = colon + 1 + length
return res