Skip to content

Commit b4d98cc

Browse files
committed
encode and decode strings solution
1 parent 0138b06 commit b4d98cc

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
"""
3+
@param: strs: a list of strings
4+
@return: encodes a list of strings to a single string.
5+
"""
6+
7+
def encode(self, strs):
8+
# write your code here
9+
result = ""
10+
for s in strs:
11+
result += str(len(s)) + "@" + s
12+
return result
13+
14+
"""
15+
@param: str: A string
16+
@return: decodes a single string to a list of strings
17+
"""
18+
19+
def decode(self, str):
20+
# write your code here
21+
result = []
22+
i = 0
23+
while i < len(str):
24+
j = i
25+
# ์‹œ์ž‘์  ์•„๋‹Œ ๊ฒฝ์šฐ
26+
while str[j] != "@":
27+
j += 1
28+
# ์‹œ์ž‘์ ์ธ ๊ฒฝ์šฐ
29+
length = int(str[i:j])
30+
word = str[j + 1: j + 1 + length]
31+
result.append(word)
32+
i = j + 1 + length
33+
return result

0 commit comments

Comments
ย (0)