Given a finite alphabet S, a binary code over this alphabet S is a
function that maps each element of S to some (possibly empty) string over the alphabet {0,1}.
An example of such a code for S={a,b,c,d} is the function f defined by
f(a)=1, f(b)=1010, f(c)=01, f(d)=10101.
Any binary code can be naturally extended to encode strings over the alphabet S
simply by concatenating the codes of the string's letters, in order.
For example, using the code mentioned above we can encode cac as f(cac)=01101.
A code is called ambiguous if there are two different strings over S that have the same encoding.
Obviously, in practice we want to avoid using an ambiguous code.
A code is called really ambiguous if there are three different strings over S that have the same encoding.
For example, the code from the above example is really ambiguous: the strings ba, acc, and d are all encoded to
10101.
You will be given a String[] code containing the strings over {0,1} used to encode letters of some alphabet S.
Your method should check whether this code is really ambiguous.
If it is really ambiguous, find a shortest string over {0,1} that is an encoding of (at least) three different strings over S, and return its length.
If the given code is not really ambiguous, return -1.
|