Is it a prefix code?
Pyth, 8 bytes
.AxM.PQ2
Take all 2 element permutations of the input, map each to the index of one string in the other (0 for a prefix) and return whether all results are truthy (non-zero).
Haskell, 37 bytes
f l=[x|x<-l,y<-l,zip x x==zip x y]==l
Each element x
of l
is repeated once for every element that it's a prefix of, which is exactly once for a prefix-free list, giving the original list. The prefix property is checked by zipping both lists with x
, which cuts off elements beyond the length of x
.
Java, 128 127 126 125 124 121 bytes
(Thanks @Kenny Lau, @Maltysen, @Patrick Roberts, @Joba)
Object a(String[]a){for(int i=0,j,l=a.length;i<l;i++)for(j=0;j<l;)if(i!=j&a[j++].startsWith(a[i]))return 1<0;return 1>0;}
Ungolfed
Object a(String[] a) {
for (int i = 0, j, l = a.length; i < l; i++)
for (j = 0; j < l;)
if (i != j & a[j++].startsWith(a[i])) return 1<0;
return 1>0;
}
Output
[Hello, World]
true
[Code, Golf, Is, Cool]
true
[1, 2, 3, 4, 5]
true
[This, test, case, is, true]
true
[111, 010, 000, 1101, 1010, 1000, 0111, 0010, 1011, 0110, 11001, 00110, 10011, 11000, 00111, 10010]
true
[4, 42]
false
[1, 2, 3, 34]
false
[This, test, case, is, false, t]
false
[He, said, Hello]
false
[0, 00, 00001]
false
[Duplicate, Duplicate, Keys, Keys]
false