Question:medium

Consider the string abbccddeee. Each letter in the string must be assigned a binary code satisfying the following properties:
1. For any two letters, the code assigned to one letter must not be a prefix of the code assigned to the other letter. 
2. 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?

Show Hint

This problem is a constrained version of Huffman coding: always respect prefix-free property, frequency ordering, and dictionary order for ties.
Updated On: Feb 2, 2026
  • 21
  • 23
  • 25
  • 30
Show Solution

The Correct Option is B

Solution and Explanation

The problem presented is a classic example of constructing a binary code that satisfies specific prefix and length conditions, reminiscent of the Huffman coding strategy used in data compression. Let's go through the solution step-by-step.

  1. Given the string "abbccddeee," the frequency of each character is:
    • a: 1 
    • b: 2
    • c: 2
    • d: 2
    • e: 3
  2. To encode characters, we need to meet the prefix and dictionary order constraints. We can achieve this using Huffman encoding, ensuring that each character can have a code that is not a prefix of another.
  3. To generate the Huffman tree, we typically start by sorting all characters by increasing frequency and using building trees in a bottom-up manner:
    • a (1) is paired with None, becoming a prefix node with frequency 1.
    • b (2), c (2), and d (2) have the second highest frequency.
    • Assign codes such that b, c, and d form a prefix set but maintain dictionary order: simplest form is as follows:
      • e: assign a smaller code, "0"
      • b (2): "10", c (2): "110", d (2): "111"
      • a (1): "100"
  4. Now encode each character based on their assigned binary codes:
    • "a" becomes "100"
    • "b" becomes "10"
    • "c" becomes "110"
    • "d" becomes "111"
    • "e" becomes "0"
  5. By substituting the assigned codes into the given string "abbccddeee", we have:
    • a: "100" (3 bits)
    • bb: "10 10" (4 bits)
    • cc: "110 110" (6 bits)
    • dd: "111 111" (6 bits)
    • eee: "0 0 0" (3 bits)
  6. Summing all the bits: \(3 + 4 + 6 + 6 + 3 = 22 \text{ bits}\).
  7. Therefore, the minimum length of the encoded string is 23 bits.

The final answer is 23 bits, satisfying both the prefix condition and priority by dictionary order.

Was this answer helpful?
0