Run-length encoding (RLE) is a simple lossless compression scheme that replaces each run of one repeated character with a count followed by that character, so aaab becomes 3a1b. It shines on data with long stretches of the same value — scanned faxes, simple bitmaps, sensor logs — and is a classic warm-up because the encode and decode halves are short but full of small traps. You will implement both, packaged on one object as runLengthEncoding = { encode, decode }. See run-length encoding for background.
runLengthEncoding.encode(str) // -> compressed string, e.g. '3a2b1c'
runLengthEncoding.decode(str) // -> original string; the exact inverse of encode
Each run in the output is written as a count immediately followed by its character — for example 3a for three as. The count is always present, even when it is 1.
runLengthEncoding.encode('aaabbc'); // '3a2b1c'
runLengthEncoding.encode('wwwwaaadexxxxxx'); // '4w3a1d1e6x'
runLengthEncoding.encode(''); // ''
runLengthEncoding.decode('3a2b1c'); // 'aaabbc'
runLengthEncoding.decode('12a3b'); // 'aaaaaaaaaaaabbb' (12 a's, then 3 b's)
runLengthEncoding.decode(runLengthEncoding.encode('mississippi')); // 'mississippi'
abc encodes to 1a1b1c. Writing the count always keeps decode a simple alternating read.as encodes to 12a. On decode you must read the whole number before the character, not just the first digit.aabaa is three runs, 2a1b2a, not a merged pair of a runs.encode('') and decode('') are both ''.decode(encode(s)) must equal s for every digit-free string s. Build both functions yourself; no libraries.