File tree 1 file changed +30
-0
lines changed
encode-and-decode-strings
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ # ๋ฌธ์ : https://neetcode.io/problems/string-encode-and-decode
2
+ # TC: O(N), SC: O(1)
3
+ # ASCII ๋ฌธ์์ด์ด ์๋ ์ด๋ชจํฐ์ฝ์ผ๋ก ๊ตฌ๋ถ์ ์ ํ
4
+
5
+ class Solution :
6
+ def encode (self , strs : List [str ]) -> str :
7
+ return '๐ค' .join (strs )
8
+
9
+ def decode (self , s : str ) -> List [str ]:
10
+ return s .split ('๐ค' )
11
+
12
+ # ASCII ๋ฌธ์์ด์ ํฌํจ๋ ๊ธฐํธ๋ก ๊ตฌ๋ถ์๋ฅผ ์จ์ผํ ๋
13
+ # -> ๊ธ์ ์ ํ์
14
+ class Solution :
15
+ def encode (self , strs : List [str ]) -> str :
16
+ text = ""
17
+ for str in strs :
18
+ text += f"{ len (str )} :{ str } "
19
+ return text
20
+
21
+ def decode (self , s : str ) -> List [str ]:
22
+ ls , start = [], 0
23
+ while start < len (s ):
24
+ mid = s .find (":" , start )
25
+ length = int (s [start : mid ])
26
+ word = s [mid + 1 : mid + 1 + length ]
27
+ ls .append (word )
28
+ start = mid + 1 + length
29
+ return ls
30
+
You canโt perform that action at this time.
0 commit comments