Skip to content

Commit 905eb33

Browse files
committed
encode and decode strings solution
1 parent 6328dc6 commit 905eb33

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Codec:
2+
def encode(self, strs: List[str]) -> str:
3+
"""Encodes a list of strings to a single string.
4+
"""
5+
encoded = ""
6+
for s in strs:
7+
encoded += f"{len(s):03d}{s}"
8+
return encoded
9+
10+
def decode(self, s: str) -> List[str]:
11+
"""Decodes a single string to a list of strings.
12+
"""
13+
14+
decoded = []
15+
i = 0
16+
while i < len(s):
17+
s_start_idx = i + 3
18+
s_len = int(s[i:s_start_idx])
19+
decoded.append(s[s_start_idx:s_start_idx + s_len])
20+
21+
i = s_start_idx + s_len
22+
23+
return decoded
24+
25+
26+
# Your Codec object will be instantiated and called as such:
27+
# codec = Codec()
28+
# codec.decode(codec.encode(strs))

0 commit comments

Comments
 (0)