Run-length encoding · ランルースエンコーディング
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.
ラン長符号化
ストリングを圧縮します。同じ文字の連続する部分(run)を、その文字 followed by 繰り返しの回数に置き換えます: aaabbc → a3b2c1 。回数が 1 でも必ず書き出します。
ストリングを巡り、現在の文字とそのrunの長さを追跡します。次の文字が異なれば、runを書き出して(文字+回数)、新しいrunを始めます。ループ終了後に最後のrunを書き出すのを忘れないでください。
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. · 文字列run_length_encode(s)を記述し、連続する同じ文字の列を「その文字+出現回数」に圧縮する。例えば、aaabbcがa3b2c1になるようにする。回数が1であっても必ず数字を含める。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。