File tree Expand file tree Collapse file tree
encode-and-decode-strings Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+
4+ class Solution :
5+ """
6+ ๊ฐ ๋ฌธ์์ด์ '๋ฌธ์์ด ๊ธธ์ด#๋ฌธ์์ด' ํ์์ผ๋ก ์ธ์ฝ๋ฉํ๋ค.
7+
8+ ์:
9+ ["Hello", "World", ""]
10+ -> "5#Hello5#World0#"
11+
12+ ์๊ฐ ๋ณต์ก๋: O(n)
13+ ๊ณต๊ฐ ๋ณต์ก๋: O(n)
14+
15+ n์ ๋ชจ๋ ๋ฌธ์์ด ๊ธธ์ด์ ํฉ์ด๋ค.
16+ """
17+
18+ def encode (self , strs : List [str ]) -> str :
19+ encoded = []
20+
21+ for word in strs :
22+ encoded .append (f"{ len (word )} #{ word } " )
23+
24+ return "" .join (encoded )
25+
26+ def decode (self , encoded_string : str ) -> List [str ]:
27+ decoded = []
28+ index = 0
29+
30+ while index < len (encoded_string ):
31+ delimiter_index = index
32+
33+ # ๋ฌธ์์ด ๊ธธ์ด์ ์ค์ ๋ฌธ์์ด์ ๋๋๋ '#'์ ์ฐพ๋๋ค.
34+ while encoded_string [delimiter_index ] != "#" :
35+ delimiter_index += 1
36+
37+ word_length = int (
38+ encoded_string [index :delimiter_index ]
39+ )
40+
41+ word_start = delimiter_index + 1
42+ word_end = word_start + word_length
43+
44+ decoded .append (encoded_string [word_start :word_end ])
45+
46+ # ๋ค์ ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ ์์๋๋ ์์น๋ก ์ด๋ํ๋ค.
47+ index = word_end
48+
49+ return decoded
You canโt perform that action at this time.
0 commit comments