49 lines
1.1 KiB
Python
49 lines
1.1 KiB
Python
##################### Section ##############################
|
|
# Section topic: Arrays
|
|
#
|
|
# Design an algorithm to encode a list of strings to a single string. The encoded string is then decoded back to the original list of strings.
|
|
#
|
|
# Please implement encode and decode
|
|
#
|
|
# Example 1:
|
|
#
|
|
# Input: ["neet","code","love","you"]
|
|
#
|
|
# Output:["neet","code","love","you"]
|
|
# Example 2:
|
|
#
|
|
# Input: ["we","say",":","yes"]
|
|
#
|
|
# Output: ["we","say",":","yes"]
|
|
# Constraints:
|
|
#
|
|
# 0 <= strs.length < 100
|
|
# 0 <= strs[i].length < 200
|
|
# strs[i] contains only UTF-8 characters.
|
|
#
|
|
############################################################
|
|
from typing import List
|
|
|
|
|
|
class Solution:
|
|
def encode(self, strs: List[str]) -> str:
|
|
delim: str = "-+-"
|
|
res: str = ""
|
|
counts: str = ""
|
|
encoded: str = ""
|
|
for elem in strs:
|
|
counts += f"{len(elem)}{delim}"
|
|
encoded += elem
|
|
counts += delim
|
|
res = counts + encoded
|
|
return encoded
|
|
|
|
|
|
def decode(self, s: str) -> List[str]:
|
|
pass
|
|
|
|
|
|
s = Solution()
|
|
|
|
print(s.encode(strs=["we", "say", ":", "yes"]))
|
|
# s.decode("we say: yes")
|