Created
April 24, 2022 06:35
-
-
Save theabbie/955eb0976a3ebba6f2786bbd0e23cc6a to your computer and use it in GitHub Desktop.
Encode and Decode Strings Leetcode Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
""" | |
@param: strs: a list of strings | |
@return: encodes a list of strings to a single string. | |
""" | |
def encode(self, strs): | |
_delim = "!@#$%^&*" | |
delim = _delim | |
check = "".join(strs) | |
i = 1 | |
while delim in strs: | |
delim += _delim | |
i += 1 | |
return "#" * i + delim.join(strs) | |
""" | |
@param: str: A string | |
@return: dcodes a single string to a list of strings | |
""" | |
def decode(self, s): | |
delim = "!@#$%^&*" | |
i = 0 | |
while s[i] == '#': | |
i += 1 | |
return s[i:].split(delim * i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment