forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkangdaia.py
More file actions
37 lines (32 loc) ยท 928 Bytes
/
kangdaia.py
File metadata and controls
37 lines (32 loc) ยท 928 Bytes
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
class Solution:
def encode(self, strs: list[str]) -> str:
"""
์ธ์ฝ๋ฉ ํจ์
๋ฐฉ์: %๋ฅผ ์ฌ์ฉํด ๋ฌธ์์ด์ ๊ธธ์ด + % + ๋ฌธ์์ด๋ก ์ธ์ฝ๋ฉ
Args:
strs (list[str]): ์ธ์ฝ๋ฉํ ๋ฌธ์์ด ๋ชฉ๋ก
Returns:
str: ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด
"""
return "".join(f"{len(s)}%{s}" for s in strs)
def decode(self, s: str) -> list[str]:
"""
๋์ฝ๋ฉ ํจ์
์๊ฐ๋ณต์ก๋: O(N)
Args:
s (str): ๋์ฝ๋ฉํ ๋ฌธ์์ด
Returns:
list[str]: ๋์ฝ๋ฉ๋ ๋ฌธ์์ด ๋ชฉ๋ก
"""
res = []
i = 0
while i < len(s):
j = i
while s[j] != "%":
j += 1
length = int(s[i:j])
start = j + 1
end = start + length
res.append(s[start:end])
i = end
return res