LZW Coding

medium · compression, lzw, dictionary

LZW Coding

Implement LZW compression and decompression with a fixed maximum dictionary size.

Functions

func LZWCompress(data []byte) []int
func LZWDecompress(codes []int) []byte

Rules

  • Initial dictionary contains all single-byte strings (0..255).
  • Next available code starts at 256.
  • Maximum dictionary size is 4096. When full, stop adding new entries.
  • Encoder emits codes for the longest dictionary phrase seen.
  • Decoder must handle the KwKwK case where a code refers to the entry being defined.

Notes

  • If data is empty, return an empty slice.
  • Inputs are bytes; outputs are integer codes.
Run tests to see results
No issues detected