array and hashmap July 18, 2022

Encode and decode string

Time O(n) Space O(1) Open original problem

We will join the array using $$ as delimeter, and also split the string with $$ to get it back.

from typing import List

class Solution:
    def encode(self, s: List[str]) -> str:
        return '$$'.join(s)

    def decode(self, s: str) -> List[str]:
        return s.split('$$')

Time Complexity: O(n), where n is the length of the array
Space Complexity: O(1)