Text repeats itself, so why store the repeats? LZ77 — the heart of ZIP, PNG, and gzip — slides a window over the data and, whenever it sees something it has seen before, replaces it with a tiny pointer BACK to the earlier copy: ‘go back 12, copy 5’. The past compresses the present. Slide through the text and watch the matches fold.
LZ77 keeps a sliding WINDOW of recent text. At each position it searches the window for the longest run that also matches the upcoming text; a match of length ≥3 is emitted as a triple (offset back, length, next char), otherwise a single literal. To decompress you just replay the pointers — copying from what you already rebuilt. A fail-loud self-check throws unless decompress(compress(text)) returns the ORIGINAL exactly AND repetitive text yields fewer tokens than characters (Ziv & Lempel, 1977).
A small 32-char window and greedy longest-match here; real DEFLATE adds a 32 KB window, Huffman-coded tokens, and lazy matching. The window search and the lossless round-trip are exact.