forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhi-rachel.py
More file actions
30 lines (25 loc) ยท 904 Bytes
/
hi-rachel.py
File metadata and controls
30 lines (25 loc) ยท 904 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
# ๋ฌธ์ : https://neetcode.io/problems/string-encode-and-decode
# TC: O(N), SC: O(1)
# ASCII ๋ฌธ์์ด์ด ์๋ ์ด๋ชจํฐ์ฝ์ผ๋ก ๊ตฌ๋ถ์ ์ ํ
class Solution:
def encode(self, strs: List[str]) -> str:
return '๐ค'.join(strs)
def decode(self, s: str) -> List[str]:
return s.split('๐ค')
# ASCII ๋ฌธ์์ด์ ํฌํจ๋ ๊ธฐํธ๋ก ๊ตฌ๋ถ์๋ฅผ ์จ์ผํ ๋
# -> ๊ธ์ ์ ํ์
class Solution:
def encode(self, strs: List[str]) -> str:
text = ""
for str in strs:
text += f"{len(str)}:{str}"
return text
def decode(self, s: str) -> List[str]:
ls, start = [], 0
while start < len(s):
mid = s.find(":", start)
length = int(s[start : mid])
word = s[mid + 1 : mid + 1 + length]
ls.append(word)
start = mid + 1 + length
return ls