File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ """
3
+ @param: strs: a list of strings
4
+ @return: encodes a list of strings to a single string.
5
+ """
6
+ def encode (self , strs ):
7
+ # write your code here
8
+ answer = ""
9
+
10
+ # ๋ฌธ์์ด ์์ "๋ฌธ์์ด๊ธธ์ด + :" ๋ถ์ฌ์ ์ธ์ฝ๋ฉ
11
+ for i in strs :
12
+ answer += str (len (i )) + ":" + i
13
+
14
+ return answer
15
+
16
+ """
17
+ @param: str: A string
18
+ @return: decodes a single string to a list of strings
19
+ """
20
+ def decode (self , s ): # ๋ณ์๋ช
str์ s๋ก ์์
21
+ # write your code here
22
+ answer = []
23
+ i = 0
24
+
25
+ # ":"๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ ์ซ์(๋ฌธ์์ด๊ธธ์ด)๋งํผ ๋์ด์ ๋์ฝ๋ฉ
26
+ while i < len (s ):
27
+ j = i
28
+
29
+ # ๋ฌธ์์ด๊ธธ์ด๊ฐ ํ์๋ฆฌ ์ด์์ผ ์ ์์ผ๋๊น ":"๋ฅผ ๊ธฐ์ค์ผ๋ก ํจ
30
+ while s [j ] != ":" :
31
+ j += 1
32
+
33
+ length = int (s [i :j ]) # ๋ฌธ์์ด์ ๊ธธ์ด ์ถ์ถ
34
+ answer .append (s [j + 1 :j + 1 + length ]) # ๊ธธ์ด๋งํผ ๋ฌธ์์ด ์ถ์ถํด์ answer์ ๋ฃ๊ธฐ
35
+ i = j + 1 + length # ์๋ผ์ง ๋ฌธ์์ด ๋ค๋ก ์ด๋
36
+
37
+ return answer
You canโt perform that action at this time.
0 commit comments