forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsun912.py
More file actions
30 lines (28 loc) · 746 Bytes
/
sun912.py
File metadata and controls
30 lines (28 loc) · 746 Bytes
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
"""
TC: O(n)
"""
class Solution:
"""
@param: strs: a list of strings
@return: encodes a list of strings to a single string.
"""
def encode(self, strs):
result = ""
for str in strs:
result += str(len(str)) + "#" + str
return result
"""
@param: str: A string
@return: decodes a single string to a list of strings
"""
def decode(self, str):
result = []
idx = 0
while idx < len(str):
temp_idx = idx
while str[temp_idx] != "#":
temp_idx += 1
length = int(str[idx:temp_idx])
result.append(str[temp_idx+1:temp_idx+length+1])
idx = temp_idx + length + 1
return result