1
+ '''
2
+ ๋ชฉํ : ๋ฌธ์์ด ๋ฆฌ์คํธ๋ฅผ ํ๋์ ๋ฌธ์์ด๋ก ์ธ์ฝ๋ฉํ๊ณ , ๋ค์ ์๋ ๋ฆฌ์คํธ๋ก ๋์ฝ๋ฉํ๋ ์๊ณ ๋ฆฌ๋ฌ์ ๋ง๋๋ ๋ฌธ์ ์
3
+ ํต์ฌ : ๋ฌธ์์ด์ ์ด๋ค ๋ฌธ์๊ฐ ๋ค์ด ์๋๋ผ๋ ์ ํํ ์ธ์ฝ๋ฉ/๋์ฝ๋ฉํ ์ ์์ด์ผ ํจ
4
+ ํด๊ฒฐ๋ฒ : ๊ฐ ๋ฌธ์์ด ์์ ๊ธธ์ด๋ฅผ ๋ถ์ฌ์ ์ธ์ฝ๋ฉ ํจ
5
+
6
+ Example 1. ๋จ๊ณ๋ณ ์ค๋ช
7
+ ์
๋ ฅ: ["lint","code","love","you"]
8
+
9
+ ์ธ์ฝ๋ฉ ๊ณผ์ :
10
+ "lint" โ 4:lint
11
+ "code" โ 4:code
12
+ "love" โ 4:love
13
+ "you" โ 3:you
14
+
15
+ ์ต์ข
์ธ์ฝ๋ฉ ๋ฌธ์์ด: 4:lint4:code4:love3:you
16
+
17
+ ๋์ฝ๋ฉ ๊ณผ์ :
18
+ i=0 โ : ์์น 1, ๊ธธ์ด 4 โ ๋ฌธ์์ด lint (i=5)
19
+ i=5 โ : ์์น 6, ๊ธธ์ด 4 โ ๋ฌธ์์ด code (i=10)
20
+ i=10 โ : ์์น 11, ๊ธธ์ด 4 โ ๋ฌธ์์ด love (i=15)
21
+ i=15 โ : ์์น 16, ๊ธธ์ด 3 โ ๋ฌธ์์ด you (i=19)
22
+
23
+ ์ต์ข
๊ฒฐ๊ณผ: ["lint","code","love","you"]
24
+
25
+ ๋์ ์๋ฆฌ ์์ฝ
26
+ ์ธ์ฝ๋ฉ: ๊ฐ ๋ฌธ์์ด ์์ ๊ธธ์ด๋ฅผ ๋ถ์ฌ์ ํผ๋ ์์ด ๋์ฝ๋ฉ ๊ฐ๋ฅํจ
27
+ ๋์ฝ๋ฉ: ๊ธธ์ด ์ ๋ณด๋ฅผ ์ด์ฉํด ์ ํํ ๋ฌธ์์ด์ ์ถ์ถํจ
28
+ '''
29
+
30
+
31
+ class Solution :
32
+ def encode (self , strs ):
33
+ encoded = []
34
+ for s in strs :
35
+ encoded .append (f"{ len (s )} :{ s } " ) # ๊ฐ ๋ฌธ์์ด ์์ ๊ธธ์ด + ":"์ ๋ถ์
36
+ return "" .join (encoded ) # ๋ชจ๋ ๋ฌธ์์ด์ ํ๋๋ก ํฉ์นจ
37
+
38
+ def decode (self , s ):
39
+ decoded = []
40
+ i = 0
41
+ while i < len (s ):
42
+ colon = s .find (":" , i ) # s.find(":", i) : ํ์ฌ ์์น(i)๋ถํฐ ์ฒ์ ๋์ค๋ :์ ์์น๋ฅผ ์ฐพ๊ธฐ
43
+ length = int (s [i :colon ]) # int(s[i:colon]) : ์์ ์ซ์๋ฅผ ๋ฌธ์์ด ๊ธธ์ด๋ก ๋ณํ
44
+ i = colon + 1 # ๋ฌธ์์ด ์์ ์์น๋ก ์ด๋
45
+ decoded .append (s [i :i + length ]) # s[i:i+length : ๊ธธ์ด๋งํผ ๋ฌธ์์ด์ ์๋ผ์ ์ ์ฅํจ.
46
+ i += length # ๋ค์ ๋ฌธ์์ด์ ์์ ์์น๋ก ์ด๋
47
+ return decoded
0 commit comments