-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path271. Encode and Decode Strings.py
More file actions
59 lines (38 loc) · 1.06 KB
/
Copy path271. Encode and Decode Strings.py
File metadata and controls
59 lines (38 loc) · 1.06 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
59
class Solution:
def encode(self, strs: List[str]) -> str:
# length + #
encodedString = ""
for string in strs:
encodedString += str(len(string)) + "#" + string
return encodedString
def decode(self, s: str) -> List[str]:
result = []
i = 0
j = 0
while i < len(s):
if s[i] != "#":
i += 1
continue
# we should be on the '#'
length = int(s[j:i])
j = i + length + 1
# string split to grab individual string
result.append(s[i+1:j])
# update pointer past string we extracted
i = j
return result
'''
list of strings -> string -> list of strings
encode:
can easily just join the list and we will get the encoded
decode:
where to split the strings?
["abc","bc"]
store length of string + some seperator
3#abc2#bc
|
string[i+1,i+3+1] = abc --> add this to a list
3#abc2#bc
|
|
'''