150 principais entrevistasFácil

Codificar e decodificar strings

Guia detalhado e implementação de Python para o problema de 'Codificar e decodificar strings'.

Declaração do problema

Fácil

Projete um algoritmo para codificar uma lista de strings em uma única string. A string codificada é então decodificada de volta à lista original de strings.

Implemente duas funções:

- encode(strs: List[str]) -> str — Codifica uma lista de strings em uma única string.

- decode(s: str) -> List[str] — Decodifica uma única string de volta à lista original de strings.

A string codificada deve ser capaz de lidar com quaisquer caracteres possíveis nas strings de entrada, incluindo caracteres especiais e strings vazias.

Restrições
  • 0 <= len(strs) <= 200
  • 0 <= len(strs[i]) <= 200
  • strs[i] can contain any possible characters (0-255)

Exemplos

Example 1
Input
strs = ["hello", "world"]
Output
["hello", "world"]
Explanation

The list is encoded into a single string and then decoded back to the original list ["hello", "world"].

Example 2
Input
strs = [""]
Output
[""]
Explanation

A list containing a single empty string is encoded and decoded correctly.

Example 3
Input
strs = ["we", "say", ":", "yes"]
Output
["we", "say", ":", "yes"]
Explanation

Special characters like ":" are handled correctly during encoding and decoding.

Need a Hint?
Considere o uso de estruturas de dados específicas de arrays e hashing, como conjuntos ou heaps.
Edge Cases to Watch
  • Estruturas de entrada vazias
  • Entradas de elemento único
  • Grandes limites numéricos

Pronto para resolver?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Abrir no Editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Recursos Python recomendados

Expanda seu conhecimento com tutoriais interativos relacionados, folhas de dicas e comparações de código.