Encode and Decode Strings

Problem

https://leetcode.com/problems/encode-and-decode-strings/

Pattern

Array, String, Design

Approaches

Code

class Codec:

    def encode(self, strs: list[str]) -> str:
        if strs == []:
            return ""

        s = ""
        for t in strs:
            s += str(len(t)) + "#" + t
        return s

    def decode(self, s: str) -> list[str]:
        if s == "":
            return []

        strs = []
        i = 0
        while i < len(s):
            j = i
            while s[j] != "#":
                j += 1
            length = int(s[i:j])
            i = j + 1
            strs.append(s[i : i + length])
            i = i + length

        return strs

Test

>>> from encode_and_decode_strings__length_prefix import Codec
>>> c = Codec()
>>> c.decode(c.encode(["hello", "world"]))
['hello', 'world']
>>> c.decode(c.encode([""]))
['']
>>> c.decode(c.encode([]))
[]
class encode_and_decode_strings__length_prefix.Codec

Bases: object

decode(s: str) list[str]
encode(strs: list[str]) str