Run-length encoding · Codificación por longitud de carrera
Run-length encoding
Compress a string by replacing each run of the same character with that character followed by how many times it repeats: aaabbc → a3b2c1. Always write the count, even when it is 1.
Walk the string tracking the current character and how long its run is. When the next character differs, write out the run (character + count) and start a fresh run. Don't forget to write the last run after the loop ends.
Codificación por longitudes de secuencia
Comprime una cadena reemplazando cada secuencia de caracteres idénticos por ese carácter seguido de cuántas veces se repite: aaabbc → a3b2c1. Escribe siempre el conteo, incluso cuando sea 1.
Recorre la cadena manteniendo el carácter actual y la longitud de su secuencia. Cuando el siguiente carácter difiere, escribe la secuencia (carácter + conteo) e inicia una nueva secuencia. No olvides escribir la última secuencia después de que termine el bucle.
Write run_length_encode(s) that compresses runs of the same character into the character followed by its run length, e.g. aaabbc becomes a3b2c1. Always include the count, even when it is 1. · Escribe run_length_encode(s) que comprima las secuencias del mismo carácter en el carácter seguido de su longitud de carrera, p. ej. aaabbc se convierte en a3b2c1. Incluye siempre el conteo, incluso cuando es 1.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.