Skip to content

Files

Latest commit

Nov 29, 2022
fed47c5 · Nov 29, 2022

History

History

271-EncodeandDecodeStrings

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Nov 29, 2022
Nov 29, 2022

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)