File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ ๏ปฟ๏ปฟ #ํด์
2+ #encodeํจ์: ๋งค๊ฐ๋ณ์ strs ๋ฆฌ์คํธ๋ฅผ join ๋ฉ์๋์ ํน์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํด ํ๋์ string์ธ answer๋ก ์ ํ
3+ #decodeํจ์: ๋งค๊ฐ๋ณ์ string s๋ฅผ split ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ํน์ ๋งค๊ฐ๋ณ์๋ฅผ ๊ธฐ์ ์ผ๋ก ๋๋์ด list๋ก ์ ํํ์ฌ returnํ๋ค.
4+ # ๋ง์ฝ strs๊ฐ ๋น์ด์์๋๋ ํน์ string์ ์ฃผ์
ํ์ฌ decode ์์ ํด๋น string์ ์ธ์ํ์ฌ ๋น ๋ฐฐ์ด([])๋ฅผ returnํ๋ค.
5+
6+
7+ #Big O
8+ #N: ๋ฆฌ์คํธ strs์ ๊ธธ์ด (element ๊ฐฏ์)
9+ #L: strs์ ๊ฐ element ํ๊ท ๊ธธ์ด (๋ฌธ์์ด์ ๊ธธ์ด)
10+ #M: string s ์ ๊ธธ์ด
11+
12+ #Time Complexity:
13+ #-encode: O(N*L)
14+ #-- join(strs): ๋ฆฌ์คํธ์ ์๋ N๊ฐ์ element์ ๊ฐ ๋ฌธ์์ด์ ๊ธธ์ด L์ ํฉ์ฐํ์ฌ ๋ฌธ์์ด ์์ฑ -> O(N * L)
15+ #-decode: O(M):
16+ #- split('๊ตฌ๋ถ์'): split ๋ฉ์๋๋ ๊ตฌ๋ถ์๋ฅผ ์ฐพ๋ ๊ณผ์ ์์ string s๋ฅผ ์ํํ๋ฏ๋ก -> O(M)
17+
18+
19+
20+ #Space Complexity: O(1)
21+ #-encode: O(N*L)
22+ #-- answer: join ๋ฉ์๋๋ก ์์ฑ๋๋ ๋ฌธ์์ด์ strs ๋ฆฌ์คํธ์ ๋ชจ๋ ๋ฌธ์์ด์ ํฉ์น ๊ฐ์ด๋ฏ๋ก -> O(N * L)
23+ #-decode: O(M)
24+ #-- answer:split ๋ฉ์๋๋ก ์์ฑ๋๋ ๋ฆฌ์คํธ๋ string s์ ๊ธธ์ด์ ๋น๋กํ์ฌ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง -> O(M)
25+
26+
27+
28+ class Solution :
29+
30+
31+ def encode (self , strs : List [str ]) -> str :
32+ answer = '!@#$%123456789' .join (strs )
33+ if len (strs ) == 0 :
34+ answer = "I am empty"
35+ return answer
36+
37+ def decode (self , s : str ) -> List [str ]:
38+ answer = s .split ('!@#$%123456789' )
39+ if s == "I am empty" :
40+ answer = []
41+ return answer
42+
43+
44+
You canโt perform that action at this time.
0 commit comments