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?
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? Correct Answer 23
Answer: Option 1
Data:
abbccddeee
frequencies are
|
Letter |
Frequency |
|
a |
1 |
|
b |
2 |
|
c |
2 |
|
d |
2 |
|
e |
3 |
[ alt="F1 Raju.S 27-03-21 savita D7" src="//storage.googleapis.com/tb-img/production/21/03/F1_Raju.S_27-03-21_savita_D7.png" style="width: 273px; height: 253px;">
codes :
|
Letter |
code |
Frequency |
|
a |
000 |
1 |
|
b |
11 |
2 |
|
c |
10 |
2 |
|
d |
001 |
2 |
|
e |
01 |
3 |
Hence length of the encoded string = 3×1 + 2× 2 + 2× 2 + 3× 2 + 2× 3 = 23
Note:
Those Binary codes satisfy both the condition given in the question.
for example, consider the following codes
a=000
b=001
c=10
d=11
e=01; then this violates the second condition because b alphabetically comes before c; b and c have the same frequency and according to question b should have length at most c and d but here the length of b is greater than c and d.