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)