← Years 9–10 Data & Information

Run-length encoding: encoding text

Run-length encoding: each run of identical characters becomes a count then the character, so AAAABBB is written 4A3B. A single character is written 1X.

Understanding Run-length encoding: encoding text in Years 9–10 Data & Information

What it does: The lone B is a run of length 1, so it is written 1B. This keeps every run in the same count-then-character format. There are three runs: 2 X's, 3 Y's and 4 Z's. Writing each count and character in order gives 2X3Y4Z. A run is a block of identical characters in a row. WWW, BB and WWWW are three separate runs, even though W appears in two of them. The single B is still a run of length 1, so it must be written as 1B. Leaving it out loses data. The count is the number of characters in the run, which is 12. So the run is written as 12H.

Common mistakes: Run-length encoding writes the count first, then the character. The output should be 4A3B, not A4B3. A single character still needs a count of 1. The lone B must be written 1B, so the output is 5W1B.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 6–16) and word search (Modules 6–132).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
What is AAAABBB run-length encoded?
A4B34A3B
Matches
AAAABBB → A4B3
↓
The character is written before the count. The count must come first, so it should be 4A3B.
Applying
Meanings
Term
4A3B
Meaning
Run-length encoding: each run of identical characters becomes a count then the character, so AAAABBB is written 4A3B. A single character is written 1X.
Practice
Text: AAAABBBCCD
Testing
Gaps
Encode AAAABBB: A3B
Check Answer
Spots
3Counts: 4 and 3
4Output: A4B3

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Pufferfish' badge — Error Handler, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. What is AAAABBB run-length encoded?
A. 4A3B ✓
B. A4B3
In this journey, each run is written as the count first, then the character. Four As and three Bs gives 4A3B.
2. How many runs are in the string WWWBBWWWW?
A. 3 runs ✓
B. 2 runs
A run is a block of identical characters in a row. WWW, BB and WWWW are three separate runs, even though W appears in two of them.
3. Which is the correct run-length encoding of the string CCCCCC?
A. C6
B. 6C ✓
Write the count first, then the character. Six Cs in a row is 6C.
4. How is a single X on its own written in run-length encoding?
A. 1X ✓
B. X
Every run gets a count, even a run of length 1. A lone X is written 1X.
5. Which is the correct encoding of AABCCC?
A. 2A3C
B. 2A1B3C ✓
The single B is still a run of length 1, so it must be written as 1B. Leaving it out loses data.

Match the Pairs

AAAABBB → 4A3B → Each run of identical characters is replaced by a count followed by the character
WWWWWWWW → 8W → One run of 8 identical characters is written as the count 8 then the letter W
A single character that is not repeated still gets a count of 1 → AAABCC → 3A1B2C
Count the runs first, then write each count followed by its character → XXYYYZZZZ → 2X3Y4Z
AAAABBB → A4B3 → The character is written before the count. The count must come first, so it should be 4A3B.
AABCCC → 2AB3C → The single B is missing its count. It should be written 1B, so the answer is 2A1B3C.

Key Term

4A3B
Run-length encoding: each run of identical characters becomes a count then the character, so AAAABBB is written 4A3B. A single character is written 1X.

Fill in the Blanks

1. Encode AAAABBB: 4A3B
Hint: Type the count of the first run of identical letters.
2. Encode WWWWWWBB: 6W2B
Hint: Type how many B characters are in the second run.
3. Encode ABBB: 1A3B
Hint: Type the count written for a single character that is not repeated.
4. Encode CCCDD: 3C2D
Hint: Type the character that makes up the first run.
5. Encode AABAAA: 2A1B3A
Hint: Type the count for the last run. Runs are counted separately, even for the same letter.

Spot the Error

Question 1
A Input: AAAABBB
B Runs: AAAA, BBB
C Counts: 4 and 3
D Output: A4B3
Line D — Run-length encoding writes the count first, then the character. The output should be 4A3B, not A4B3.
Question 2
A Input: WWWWWB
B Runs: WWWWW, B
C Output: 5WB
Line C — A single character still needs a count of 1. The lone B must be written 1B, so the output is 5W1B.
Question 3
A Input: CCDDDDE
B Runs: CC, DDDD, E
C Counts: 2, 4, 1
D Output: 2C3D1E
Line D — The run of D is four letters long, as the Counts line says. It should be 4D, so the output is 2C4D1E.
Question 4
A Input: ZZZZZZ
B Run length: 5
C Output: 6Z
Line B — ZZZZZZ has six Z characters, so the run length is 6, not 5. This matches the output 6Z.
Question 5
A text = AAABCC
B runs = AAA, B, CC
C encoded = 3A2B2C
Line C — There is only one B in the text, so it should be written 1B. The encoded string should be 3A1B2C.

Student with a class code? Join your class →