forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbhyun-kim.py
More file actions
34 lines (25 loc) · 810 Bytes
/
bhyun-kim.py
File metadata and controls
34 lines (25 loc) · 810 Bytes
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
"""
271. Encode and Decode Strings
https://leetcode.com/problems/encode-and-decode-strings/description/
Solution:
- Concatenate the strings with a special character
- Split the string by the special character
Time complexity: O(n)
- Concatenating the strings: O(n)
- Splitting the string: O(n)
- Total: O(n)
Space complexity: O(n)
- Storing the output: O(n)
- Total: O(n)
"""
from typing import List
class Codec:
def encode(self, strs: List[str]) -> str:
"""Encodes a list of strings to a single string."""
output = strs[0]
for i in range(1, len(strs)):
output += "é" + strs[i]
return output
def decode(self, s: str) -> List[str]:
"""Decodes a single string to a list of strings."""
return s.split("é")