Skip to content

Instantly share code, notes, and snippets.

@theabbie
Created April 24, 2022 06:35
Show Gist options
  • Save theabbie/955eb0976a3ebba6f2786bbd0e23cc6a to your computer and use it in GitHub Desktop.
Save theabbie/955eb0976a3ebba6f2786bbd0e23cc6a to your computer and use it in GitHub Desktop.
Encode and Decode Strings Leetcode Solution
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