forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjylee2033.py
More file actions
58 lines (48 loc) · 1.51 KB
/
jylee2033.py
File metadata and controls
58 lines (48 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution:
"""
@param: strs: a list of strings
@return: encodes a list of strings to a single string.
"""
def encode(self, strs):
# write your code here
# Encode each word with its length prefix and a "#"
# ["C#", "&"] -> "2#C#1#&"
encoded_str = ""
for word in strs:
encoded_str += f"{len(word)}#{word}"
return encoded_str
"""
@param: str: A string
@return: decodes a single string to a list of strings
"""
def decode(self, str):
# write your code here
# "2#C#1#&" -> ["C#", "&"]
decoded_lst = []
char_count = 0
reading_word = False
word = ""
length_str = ""
if str == "":
return [""]
for ch in str:
if ch == "#" and not reading_word:
# Finished reading the length prefix
# Switch to word-reading mode
char_count = int(length_str)
length_str = ""
reading_word = True
elif not reading_word:
# Accumulate digits for the length prefix
length_str += ch
else:
# reading_word is True
word += ch
char_count -= 1
if char_count == 0:
reading_word = False
decoded_lst.append(word)
word = ""
return decoded_lst
# Time Complexity: O(N)
# Space Complexity: O(N)