-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_decode_strings.py
More file actions
68 lines (49 loc) · 1.71 KB
/
Copy pathencode_decode_strings.py
File metadata and controls
68 lines (49 loc) · 1.71 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
57
58
59
60
61
62
63
64
65
66
67
68
special_char = '#'
def encode(strs: list[str]) -> str:
"""
Encodes a list of strings into a single string with length prefixes.
Interview Tips:
- Use length + delimiter + string format
- Length tells us how many characters to read
- Delimiter separates length from actual string
- Key insight: need to know where each string ends
Complexity: O(n) time, O(1) space where n = total characters
"""
output = ''
for string in strs:
length = str(len(string))
output += length + special_char + string
return output
def decode(s: str) -> list[str]:
"""
Decodes a string back into a list of strings.
Interview Tips:
- Parse length before delimiter
- Extract string of that length after delimiter
- Move pointer to end of current string
- Key insight: length tells us exactly how many chars to read
Complexity: O(n) time, O(1) space where n = total characters
"""
output = []
i = 0
while i < len(s):
j = i
while s[j] != special_char:
j += 1
length = int(s[i:j])
start_index = j + 1
end_index = start_index + length
output.append(s[start_index:end_index])
i = end_index
return output
def test_encode_decode_strings():
"""Test function that can be run from terminal."""
# Test: ["hello","world"] -> encode -> decode -> ["hello","world"]
strs = ["hello", "world"]
encoded = encode(strs)
decoded = decode(encoded)
print(f"Original: {strs}")
print(f"Encoded: {encoded}")
print(f"Decoded: {decoded}")
if __name__ == "__main__":
test_encode_decode_strings()