LZ77 Compression - Problem

Implement LZ77 compression and decompression algorithms. LZ77 is a lossless data compression algorithm that uses a sliding window to find repeated patterns in the input data.

For compression, scan through the input text and for each position, find the longest match within a sliding window of previously seen characters. Output a sequence of tokens in the format (offset, length, next_char) where:

  • offset: Distance back to the start of the match (0 if no match)
  • length: Length of the matched substring (0 if no match)
  • next_char: The character following the match

For decompression, process each token and reconstruct the original text by copying characters from the previously decoded output based on the offset and length, then append the next character.

Use a sliding window size of 12 characters for the search buffer.

Input & Output

Example 1 — Basic Compression
$ Input: text = "ABABAC", operation = "compress"
Output: [[0,0,"A"],[0,0,"B"],[2,2,"C"]]
💡 Note: First A: no previous match → (0,0,A). First B: no match → (0,0,B). Second AB: matches AB at position 0-1, offset=2, length=2 → (2,2,C)
Example 2 — Decompression
$ Input: text = [[0,0,"A"],[0,0,"B"],[2,2,"C"]], operation = "decompress"
Output: ABABAC
💡 Note: Token (0,0,A): copy nothing, append A → "A". Token (0,0,B): append B → "AB". Token (2,2,C): copy 2 chars from offset 2 (AB), append C → "ABABAC"
Example 3 — No Matches Found
$ Input: text = "ABCD", operation = "compress"
Output: [[0,0,"A"],[0,0,"B"],[0,0,"C"],[0,0,"D"]]
💡 Note: Each character appears for the first time, so no matches are found. Each token has offset=0, length=0, followed by the literal character.

Constraints

  • 1 ≤ text.length ≤ 1000 (for compression)
  • 1 ≤ tokens.length ≤ 1000 (for decompression)
  • Window size is fixed at 12 characters
  • Characters are printable ASCII

Visualization

Tap to expand
INPUTALGORITHMOUTPUTOriginal TextLZ77 CompressionToken ArrayABABACABABAC1Scan for matches2Find longest match in window3Generate token (offset, length, char)4Slide window forward[0,0,"A"][0,0,"B"][2,2,"C"]Compressed tokensKey Insight:Sliding window balances compression ratio with memory efficiency by limiting search to recent charactersTutorialsPoint - LZ77 Compression | Sliding Window Algorithm
Asked in
Google 12 Amazon 8 Microsoft 6 Adobe 4
8.9K Views
Medium Frequency
~45 min Avg. Time
340 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen