Skip to content

Commit 9bdaebb

Browse files
Solve : Encode and Decode Strings
1 parent 649197a commit 9bdaebb

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def encode(self, strs):
3+
res = ''
4+
for s in strs:
5+
res += str(len(s)) + '#' + s
6+
return res
7+
8+
def decode(self, s):
9+
res = []
10+
i = 0
11+
while i < len(s):
12+
j = i
13+
while s[j] != '#':
14+
j += 1
15+
length = int(s[i:j])
16+
res.append(s[j+1:j+1+length])
17+
i = j + 1 + length
18+
return res

0 commit comments

Comments
 (0)