-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathencode_decode.py
More file actions
56 lines (42 loc) · 1.36 KB
/
encode_decode.py
File metadata and controls
56 lines (42 loc) · 1.36 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Encode and Decode Strings
Design an algorithm to encode a list of strings to a single string, and
decode it back to the original list of strings.
Reference: https://leetcode.com/problems/encode-and-decode-strings/
Complexity:
Time: O(n) for both encode and decode
Space: O(n)
"""
from __future__ import annotations
def encode(strs: str) -> str:
"""Encode a space-separated string into a length-prefixed format.
Args:
strs: A space-separated string of words.
Returns:
A single encoded string with length-prefixed words.
Examples:
>>> encode("keon is awesome")
'4:keon2:is7:awesome'
"""
result = ""
for word in strs.split():
result += str(len(word)) + ":" + word
return result
def decode(text: str) -> list[str]:
"""Decode a length-prefixed string back into a list of strings.
Args:
text: The encoded string with length-prefixed words.
Returns:
A list of the original decoded strings.
Examples:
>>> decode("4:keon2:is7:awesome")
['keon', 'is', 'awesome']
"""
words: list[str] = []
index = 0
while index < len(text):
colon_index = text.find(":", index)
size = int(text[index:colon_index])
words.append(text[colon_index + 1 : colon_index + 1 + size])
index = colon_index + 1 + size
return words