Skip to content

Latest commit

 

History

History
31 lines (25 loc) · 1 KB

File metadata and controls

31 lines (25 loc) · 1 KB

Encode and Decode Strings

Problem can be found in here!

class Codec:
    def encode(self, strs: List[str]) -> str:
        """ Encodes a list of strings to a single string """
        encoded_string = ""
        for s in strs:
            encoded_string += f"{len(s)}#{s}"
        return encoded_string

    def decode(self, s: str) -> List[str]:
        """ Decodes a single string to a list of strings """
        index = 0
        decoded_string = []
        while index < len(s):
            j = index
            while s[j] != "#":
                j += 1
            length = int(s[index:j])
            decoded_string.append(s[j+1:j+1+length])
            index = j + 1 + length

        return decoded_string

Time Complexity: O(n), Space Complexity: O(1)