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+ Solution:
3+ 1) encode: 각 글자의 앞에 글자수와 # 라는 delimiter 를 붙여 stringify 한다.
4+ 2) decode: length 를 참고삼아 word를 따내어 result 배열을 만든다.
5+
6+ Time: O(n)
7+ Space: O(n)
8+ """
9+
10+
11+ class Codec :
12+
13+ def encode (self , strs : List [str ]) -> str :
14+ """Encodes a list of strings to a single string."""
15+ result = ""
16+ for word in strs :
17+ result += str (len (word ))
18+ result += "#"
19+ result += word
20+ return result
21+
22+ def decode (self , s : str ) -> List [str ]:
23+ """Decodes a single string to a list of strings."""
24+ i = 0
25+ result = []
26+ length = ""
27+ while i < len (s ):
28+ # find number
29+ length = ""
30+ while s [i ] is not "#" :
31+ length += s [i ]
32+ i += 1
33+ # find #
34+ i += 1
35+ # find word
36+ result .append (s [i : i + int (length )])
37+ i += int (length )
38+
39+ return result
40+
41+
42+ # Your Codec object will be instantiated and called as such:
43+ # codec = Codec()
44+ # codec.decode(codec.encode(strs))
You can’t perform that action at this time.
0 commit comments