-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_decode.py
More file actions
30 lines (27 loc) · 781 Bytes
/
encode_decode.py
File metadata and controls
30 lines (27 loc) · 781 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
class Solution:
def encode(self, strs: List[str]) -> str:
final = ""
first = True
for st in strs:
if first:
first = False
final += str(len(st))
else:
final += " " + str(len(st))
final += '#'
for st in strs:
final += st
return final
def decode(self, s: str) -> List[str]:
split_index = s.index('#')
if split_index == 0:
return []
counts = s[:split_index]
string = s[split_index + 1:]
counts = [int(t) for t in counts.split(" ")]
final = []
i = 0
for count in counts:
final.append(string[i:i+count])
i += count
return final