forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeo.py
More file actions
26 lines (20 loc) · 622 Bytes
/
Leo.py
File metadata and controls
26 lines (20 loc) · 622 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
class Codec:
def encode(self, strs: List[str]) -> str:
"""Encodes a list of strings to a single string.
"""
res = ''
for s in strs:
res += str(len(s)) + '#' + s
return res
def decode(self, s: str) -> List[str]:
"""Decodes a single string to a list of strings.
"""
res = []
i = 0
while i < len(s):
a = s.find('#', i)
length = int(s[i:a])
res.append(s[a + 1:a + 1 + length])
i = a + 1 + length
return res
## TC: O(n), SC:O(n),n denotes sum of all len(s)