Run-length encoding · Codificação por comprimento de corrida
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.
Codificação por comprimento de corrida
Comprima uma string substituindo cada sequência do mesmo caractere por esse caractere seguido de quantas vezes ele se repete: aaabbc → a3b2c1. Sempre escreva o número, mesmo quando for 1.
Percorra a string acompanhando o caractere atual e quanto tempo dura sua sequência. Quando o próximo caractere diferir, escreva a sequência (caractere + número) e inicie uma nova sequência. Não esqueça de escrever a última sequência após o loop terminar.
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. · Escreva run_length_encode(s) que comprime sequências do mesmo caractere no caractere seguido pelo seu comprimento, ex. aaabbc torna-se a3b2c1. Sempre inclua a contagem, mesmo quando for 1.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.