-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode-decodeStrings.py
More file actions
56 lines (39 loc) · 1.43 KB
/
encode-decodeStrings.py
File metadata and controls
56 lines (39 loc) · 1.43 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
'''
Design an algorithm to encode a list of strings to a string.
The encoded string is then sent over the network and is decoded back to the original list of strings.
Please implement encode and decode
Example 1:
Input: ["lint","code","love","you"]
Output: ["lint","code","love","you"]
Explanation:
One possible encode method is: "lint:;code:;love:;you"
Example 2:
Input: ["we", "say", ":", "yes"]
Output: ["we", "say", ":", "yes"]
Explanation:
One possible encode method is: "we:;say:;:::;yes"
'''
def decode(strs):
# 1. Encode each word in the input with its length and a delimiter (;) before it and return it as a string:
encodedString = ""
for s in strs:
encodedString += str(len(s)) + ";" + s
print("encoded: ", encodedString)
res = []
l = 0
# Decoding:
# By using 2 pointers approach: set left pointer to 0th index and right pointer at left pointer
while l < len(encodedString):
r = l
# if delimiter found then increment r pointer
while encodedString[r] != ";":
r += 1
# find the length of the encoded words in the string
length = int(encodedString[l:r])
# append each word into the result array
res.append(encodedString[r+1: r + 1 + length])
# update the left pointer to the position before the next set of encoded word
l = r + 1 + length
return res
input = ["we;", "say", ":", "yes"]
print(decode(input))