Consider the string \(abbccddeee\). Each letter in the string must be assigned…
2021
Consider the string \(abbccddeee\). Each letter in the string must be assigned a binary code satisfying the following properties:
For any two letters, the code assigned to one letter must not be a prefix of the code assigned to the other letter.
For any two letters of the same frequency, the letter which occurs earlier in the dictionary order is assigned a code whose length is at most the length of the code assigned to the other letter.
Among the set of all binary code assignments which satisfy the above two properties, what is the minimum length of the encoded string?
- A.
21
- B.
23
- C.
25
- D.
30
Attempted by 113 students.
Show answer & explanation
Correct answer: B
Key insight: build an optimal prefix-free code (Huffman) while enforcing the tie-break rule for letters with equal frequency.
Step 1: Count frequencies: a:1, b:2, c:2, d:2, e:3.
Step 2: Perform Huffman merges, choosing merges that respect the tie-break rule (for equal frequencies, earlier dictionary letters should not end up with longer codes than later ones). One valid sequence:
- Merge a(1) with d(2) → 3; merge b(2) with c(2) → 4; merge e(3) with that 3 → 6; merge 4 and 6 → 10.
Step 3: Read off code lengths from the tree produced:
a: 3 bits, b: 2 bits, c: 2 bits, d: 3 bits, e: 2 bits.
Step 4: Compute total encoded length:
1·3 + 2·2 + 2·2 + 2·3 + 3·2 = 3 + 4 + 4 + 6 + 6 = 23 bits.
Therefore the minimum possible length of the encoded string is 23 bits.
A video solution is available for this question — log in and enroll to watch it.