Skip to content
Advanced

Run-Length Encoding

The simplest lossless compression technique.

Time Complexity
O(N)
Space Complexity
O(1)

Interactive Visualization

Playback Speed
Execution State
STEP 1 / 18
Scanning Input
A
0
A
1
A
2
A
3
A
4
B
5
B
6
B
7
C
8
C
9
D
10
A
11
A
12
RLE Compressed Buffer
...
Current CharA
Run Count1
Process Description

Starting new run for character 'A'

Technical Guide

Run-Length Encoding (RLE) is the simplest form of data compression. it replaces consecutive identical characters (a 'run') with a single character and its repeat count. For example, 'AAAAA' becomes 'A5'. It is highly effective for data with long runs of identical values, such as simple icons or sparse datasets.

Logical Phases

  • 1
    Scanning the string from left to right.
  • 2
    Counting consecutive identical characters.
  • 3
    Writing the character followed by the count to the output.
  • 4
    Handling runs of length 1 (which might increase size).

Implementation

result += char + count;

Expert Pitfalls

Using RLE on high-entropy text (like 'ABCDEFG') which will double the storage size.