forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJay-Mo-99.py
More file actions
44 lines (29 loc) ยท 1.66 KB
/
Jay-Mo-99.py
File metadata and controls
44 lines (29 loc) ยท 1.66 KB
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
38
39
40
41
42
๏ปฟ๏ปฟ #ํด์
#encodeํจ์: ๋งค๊ฐ๋ณ์ strs ๋ฆฌ์คํธ๋ฅผ join ๋ฉ์๋์ ํน์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํด ํ๋์ string์ธ answer๋ก ์ ํ
#decodeํจ์: ๋งค๊ฐ๋ณ์ string s๋ฅผ split ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ํน์ ๋งค๊ฐ๋ณ์๋ฅผ ๊ธฐ์ ์ผ๋ก ๋๋์ด list๋ก ์ ํํ์ฌ returnํ๋ค.
# ๋ง์ฝ strs๊ฐ ๋น์ด์์๋๋ ํน์ string์ ์ฃผ์
ํ์ฌ decode ์์ ํด๋น string์ ์ธ์ํ์ฌ ๋น ๋ฐฐ์ด([])๋ฅผ returnํ๋ค.
#Big O
#N: ๋ฆฌ์คํธ strs์ ๊ธธ์ด (element ๊ฐฏ์)
#L: strs์ ๊ฐ element ํ๊ท ๊ธธ์ด (๋ฌธ์์ด์ ๊ธธ์ด)
#M: string s ์ ๊ธธ์ด
#Time Complexity:
#-encode: O(N*L)
#-- join(strs): ๋ฆฌ์คํธ์ ์๋ N๊ฐ์ element์ ๊ฐ ๋ฌธ์์ด์ ๊ธธ์ด L์ ํฉ์ฐํ์ฌ ๋ฌธ์์ด ์์ฑ -> O(N * L)
#-decode: O(M):
#- split('๊ตฌ๋ถ์'): split ๋ฉ์๋๋ ๊ตฌ๋ถ์๋ฅผ ์ฐพ๋ ๊ณผ์ ์์ string s๋ฅผ ์ํํ๋ฏ๋ก -> O(M)
#Space Complexity:
#-encode: O(N*L)
#-- answer: join ๋ฉ์๋๋ก ์์ฑ๋๋ ๋ฌธ์์ด์ strs ๋ฆฌ์คํธ์ ๋ชจ๋ ๋ฌธ์์ด์ ํฉ์น ๊ฐ์ด๋ฏ๋ก -> O(N * L)
#-decode: O(M)
#-- answer:split ๋ฉ์๋๋ก ์์ฑ๋๋ ๋ฆฌ์คํธ๋ string s์ ๊ธธ์ด์ ๋น๋กํ์ฌ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง -> O(M)
class Solution:
def encode(self, strs: List[str]) -> str:
answer = '!@#$%123456789'.join(strs)
if len(strs) == 0:
answer = "I am empty"
return answer
def decode(self, s: str) -> List[str]:
answer = s.split('!@#$%123456789')
if s == "I am empty":
answer = []
return answer