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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
"""
Question
Medium
Design an algorithm to encode a list of strings to a single string.
The encoded string is then decoded back to the original list of strings.
Please implement encode and decode
Example 1:
Input: ["neet","code","love","you"]
Output:["neet","code","love","you"]
Example 2:
Input: ["we","say",":","yes"]
Output: ["we","say",":","yes"]
Constraints:
0 <= strs.length < 100
0 <= strs[i].length < 200
strs[i] contains only UTF-8 characters.
"""
from typing import Dict, List
class Solution:
"""
A class to solve string encode and decode from input list.
String encode and decode from the input strs: List[str] using a encode approach.
"""
def encode(self, strs: List[str]) -> str:
"""
string encode from the input strs: List[str] using a encode approach.
Args:
strs: List[str]: a list of strings
Returns:
str: a single string
"""
res: str = ""
for s in strs:
res += str(len(s)) + "#" + s
return res
def decode(self, s: str) -> List[str]:
"""
string decode from the input str: str using a encode approach.
Args:
s: str: a string
Returns:
res: List[str] a list of strings
"""
res: List = []
i: int = 0
while i < len(s):
j: int = i
while s[j] != "#":
j += 1
length: int = int(s[i:j])
i = j + 1
j = i + length
res.append(s[i:j])
i = j
return res
def print(self, examples: Dict) -> None:
"""
funciton to print the result.
Args:
example: Dict, a dictionary of examples
"""
for name, example in examples.items():
res_encode = self.encode(example)
print(f"{name}: {res_encode}")
res_decode = self.decode(res_encode)
print(f"{name}: {res_decode}")
def main(self):
"""
main function to call print function to test examples
"""
examples = {
"ex1": ["neet", "code", "love", "you"],
"ex2": ["we", "say", ":", "yes"],
"ex3": ["I", "love", "Yejin"],
}
# Passing the dictionary to the print method
self.print(examples)
solution = Solution()
solution.main()
"""
Solution
url: https://neetcode.io/problems/string-encode-and-decode
video: https://www.youtube.com/watch?v=B1k_sxOSgv8&embeds_referring_euri=https%3A%2F%2Fneetcode.io%2F&source_ve_path=Mjg2NjY
1. encode
time: O(n)
space: O(n)
code:
```python
class Solution:
def encode(self, strs: List[str]) -> str:
res = ""
for s in strs:
res += str(len(s)) + "#" + s
return res
def decode(self, s: str) -> List[str]:
res = []
i = 0
while i < len(s):
j = i
while s[j] != '#':
j += 1
length = int(s[i:j])
i = j + 1
j = i + length
res.append(s[i:j])
i = j
return res
```
"""
|