-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_decode_strings.py
More file actions
40 lines (29 loc) · 865 Bytes
/
encode_decode_strings.py
File metadata and controls
40 lines (29 loc) · 865 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
32
33
34
35
36
37
38
39
40
from typing import List
class Solution:
def encode(self, strings: List[str]) -> str:
s = ""
for string in strings:
s = s + f'{len(string)}#{string}'
return s
def decode(self, s: str) -> List[str]:
strings = []
i = 0
while i < len(s):
# Parse the length prefix
length_str = ''
while s[i] != '#':
length_str += s[i]
i += 1
length = int(length_str)
i += 1
string = ''
while length > 0:
string += s[i]
i += 1
length -= 1
strings.append(string)
return strings
sol = Solution()
# print(sol.encode(["neet","01234567890123456789","love"]))
# Output: 4#neet4#code#love
# print(sol.decode("4#neet4#code4#love"))