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-length encoding
บีบอัดสตริงโดยการแทนที่แต่ละ run ของตัวอักษรเดียวกันด้วยตัวอักษรนั้นตามด้วยจำนวนครั้งที่มันซ้ำ: aaabbc → a3b2c1. เขียน count เสมอ แม้ว่ามันจะเป็น 1
เดินผ่านสตริงติดตาม current character และความยาวของ run นั้น. เมื่อตัวถัดไปต่างออกไป, write out run (character + count) และเริ่ม run ใหม่. อย่าลืม write last 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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่