Le migliori 150 intervisteFacile

Codificare e decodificare stringhe

Guida dettagliata e implementazione Python per il problema "Codifica e decodifica stringhe".

Dichiarazione del problema

Facile

Progetta un algoritmo per codificare un elenco di stringhe in una singola stringa. La stringa codificata viene quindi decodificata nuovamente nell'elenco originale di stringhe.

Implementare due funzioni:

- encode(strs: List[str]) -> str — Codifica un elenco di stringhe in una singola stringa.

- decode(s: str) -> List[str] — Decodifica una singola stringa riportandola all'elenco originale di stringhe.

La stringa codificata dovrebbe essere in grado di gestire qualsiasi carattere possibile nelle stringhe di input, inclusi caratteri speciali e stringhe vuote.

Vincoli
  • 0 <= len(strs) <= 200
  • 0 <= len(strs[i]) <= 200
  • strs[i] can contain any possible characters (0-255)

Esempi

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?
Prendi in considerazione l'utilizzo di strutture dati specifiche per array e hashing come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.