Skip to content

Commit b92c6ba

Browse files
committed
Solved "Encode and decode strings"
1 parent 22af2e7 commit b92c6ba

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Constraints:
3+
1. 0 <= strs.length <= 200
4+
2. 0 <= strs[i].length <= 200
5+
3. strs[i]๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž๋“ค๋กœ๋งŒ ๊ตฌ์„ฑ๋จ
6+
4. encode๋œ string์€ ๋ฐ˜๋“œ์‹œ decode ๊ฐ€๋Šฅํ•ด์•ผ ํ•จ
7+
8+
Time Complexity: O(n)
9+
10+
Space Complexity: O(n)
11+
12+
ํ’€์ด๋ฐฉ๋ฒ•:
13+
- encode: ๊ฐ ๋ฌธ์ž์—ด ๋’ค์— '#' ์ถ”๊ฐ€ํ•˜์—ฌ ํ•˜๋‚˜๋กœ ํ•ฉ์นจ
14+
- decode: '#'์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด ๋ถ„ํ• 
15+
16+
Further consideration:
17+
- ํ˜„์žฌ ๋ฐฉ์‹์€ ์ž…๋ ฅ ๋ฌธ์ž์—ด์— '#'์ด ํฌํ•จ๋œ ๊ฒฝ์šฐ ๋ฌธ์ œ ๋ฐœ์ƒ ๊ฐ€๋Šฅ
18+
- ๊ฐœ์„  ๋ฐฉ๋ฒ•: ๋ฌธ์ž์—ด ๊ธธ์ด + ๊ตฌ๋ถ„์ž + ๋ฌธ์ž์—ด ํ˜•์‹ ์‚ฌ์šฉ
19+
์˜ˆ: ["Hello", "World"] -> "5#Hello5#World"
20+
"""
21+
22+
class Solution:
23+
"""
24+
@param: strs: a list of strings
25+
@return: encodes a list of strings to a single string.
26+
Examples:
27+
input: ["Hello", "World"]
28+
output: "Hello#World"
29+
"""
30+
def encode(self, strs):
31+
result = ""
32+
for s in strs:
33+
result = result + s + "#"
34+
return result[:-1]
35+
36+
"""
37+
@param: str: A string
38+
@return: decodes a single string to a list of strings
39+
Examples:
40+
input: "Hello#World"
41+
output: ["Hello", "World"]
42+
"""
43+
def decode(self, str):
44+
return str.split("#")

0 commit comments

Comments
ย (0)