Skip to content

Commit 1f7de95

Browse files
committed
feat: add 0238 Encode and Decode Strings solution
1 parent 52f5171 commit 1f7de95

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import List
2+
class Solution:
3+
"""
4+
@param: strs: a list of strings
5+
@return: encodes a list of strings to a single string.
6+
"""
7+
def encode(self, strs):
8+
return ''.join(f'{len(s)}#{s}' for s in strs)
9+
10+
"""
11+
@param: str: A string
12+
@return: decodes a single string to a list of strings
13+
"""
14+
def decode(self, s):
15+
16+
ans = []
17+
i = 0
18+
while i < len(s):
19+
j = i
20+
while s[j] != '#':
21+
j += 1
22+
length = int(s[i:j])
23+
start = j + 1
24+
end = start + length
25+
ans.append(s[start:end])
26+
27+
i = end
28+
return ans
29+
30+
if __name__ == "__main__":
31+
sol = Solution()
32+
33+
cases = [
34+
["abc", "a#b", "", "hello"],
35+
["", ""], # 빈 문자열 2개
36+
["#", "##", "###"], # 해시 포함
37+
]
38+
for arr in cases:
39+
enc = sol.encode(arr)
40+
dec = sol.decode(enc)
41+
print(arr == dec, arr, "->", enc[:50] + ("..." if len(enc) > 50 else ""))

0 commit comments

Comments
 (0)