Array & Hashing

57. Encode and Decode String

Design an algorithm to encode a list of strings into a single string and decode it back to the original list of strings.

The encoded string is sent over the network and later decoded back to the original list.

Requirements

  • The strings may contain any of the 256 valid ASCII characters.
  • Your algorithm must correctly handle all possible characters, including special symbols such as :, ;, etc.
  • Do not rely on libraries -- the goal is to implement the encoding and decoding logic manually.

Examples

Example 1

Input: ["lint","code","love","you"]
Output: ["lint","code","love","you"]
Explanation: One possible encoding method: "lint:;code:;love:;you"

Example 2

Input: ["we", "say", ":", "yes"]
Output: ["we", "say", ":", "yes"]
Explanation: One possible encoding method: "we:;say:;:::;yes"

Disena un algoritmo para codificar una lista de cadenas en una sola cadena y luego decodificarla para obtener la lista original.

La cadena codificada se envia a traves de la red y posteriormente se decodifica.

Requisitos

  • Las cadenas pueden contener cualquiera de los 256 caracteres ASCII validos.
  • El algoritmo debe manejar correctamente todo tipo de caracteres, incluidos simbolos especiales como :, ;, etc.
  • No se deben usar librerias; el objetivo es implementar la logica de codificacion y decodificacion desde cero.

Ejemplos

Ejemplo 1

Entrada: ["lint","code","love","you"]
Salida: ["lint","code","love","you"]
Explicacion: Un metodo posible de codificacion: "lint:;code:;love:;you"

Ejemplo 2

Entrada: ["we", "say", ":", "yes"]
Salida: ["we", "say", ":", "yes"]
Explicacion: Un metodo posible de codificacion: "we:;say:;:::;yes"
encode-and-decode-string.py
from typing import List


def encode(strs: List[str]) -> str:
    res: str = ""
    for word in strs:
        res += str(len(word)) + "#" + word
    return res


def decode(strg: str) -> List[str]:
    res: List[str] = []
    i: int = 0

    while i < len(strg):
        j = i

        # Find the '#' delimiter
        while strg[j] != "#":
            j += 1

        # Get the length of the next word
        length: int = int(strg[i:j])

        # Extract the word
        res.append(strg[j + 1 : j + 1 + length])

        # Advance the pointer past the current word
        i = j + 1 + length

    return res
Keyboard shortcuts
h Previous problem
l Next problem
Esc Back to index
? Toggle this help