Compression Algorithm - Problem
Implement a complete Huffman encoding algorithm for text compression and decompression. Your solution should build a Huffman tree from character frequencies, generate unique binary codes for each character, compress the input text, and then decompress it back to verify correctness.
The algorithm should:
- Count character frequencies in the input text
- Build a Huffman tree using a priority queue (min-heap)
- Generate binary codes for each character
- Compress the text using these codes
- Decompress the compressed data back to original text
- Calculate and return the compression ratio
Return a JSON object with: compressedBits (compressed binary string), decompressed (original text), compressionRatio (original bits / compressed bits), and codes (character to binary code mapping).
Input & Output
Example 1 — Basic Compression
$
Input:
text = "AAAABBC"
›
Output:
{"compressedBits":"00001010111","decompressed":"AAAABBC","compressionRatio":5.09,"codes":{"A":"0","B":"10","C":"11"}}
💡 Note:
A appears 4 times (most frequent) gets code '0', B appears 2 times gets '10', C appears 1 time gets '11'. Compression: A(0)+A(0)+A(0)+A(0)+B(10)+B(10)+C(11) = '00001010111'
Example 2 — Equal Frequencies
$
Input:
text = "AABB"
›
Output:
{"compressedBits":"0011","decompressed":"AABB","compressionRatio":4.0,"codes":{"A":"0","B":"1"}}
💡 Note:
A and B have equal frequency (2 each), so they get 1-bit codes. Result: A(0)+A(0)+B(1)+B(1) = '0011'
Example 3 — Single Character
$
Input:
text = "AAAA"
›
Output:
{"compressedBits":"0000","decompressed":"AAAA","compressionRatio":8.0,"codes":{"A":"0"}}
💡 Note:
Single character case: A gets code '0'. Each A becomes '0', so 'AAAA' becomes '0000'. Excellent compression ratio.
Constraints
- 0 ≤ text.length ≤ 1000
- text contains only printable ASCII characters
- Empty string is valid input
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code