Skip to content

Commit a7790ff

Browse files
committed
feat: add encode and decode functions for string manipulation
1 parent 01e0bae commit a7790ff

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def encode(self, strs: list[str]) -> str:
3+
"""
4+
인코딩 함수
5+
방식: %를 사용해 문자열의 길이 + % + 문자열로 인코딩
6+
7+
Args:
8+
strs (list[str]): 인코딩할 문자열 목록
9+
10+
Returns:
11+
str: 인코딩된 문자열
12+
"""
13+
return "".join(f"{len(s)}%{s}" for s in strs)
14+
15+
def decode(self, s: str) -> list[str]:
16+
"""
17+
디코딩 함수
18+
시간복잡도: O(N)
19+
20+
Args:
21+
s (str): 디코딩할 문자열
22+
23+
Returns:
24+
list[str]: 디코딩된 문자열 목록
25+
"""
26+
res = []
27+
i = 0
28+
while i < len(s):
29+
j = i
30+
while s[j] != "%":
31+
j += 1
32+
length = int(s[i:j])
33+
start = j + 1
34+
end = start + length
35+
res.append(s[start:end])
36+
i = end
37+
return res

0 commit comments

Comments
 (0)