[Problem Statement]
A prefix-free set is a set of words in which no element is a prefix of another element in the set. For example {"hello"} , {"hello", "goodbye", "giant", "hi"} and the empty set are examples of prefix-free sets. On the other hand, {"hello","hell"} and {"great","gig","g"} are not prefix-free. You will be given a String[] words containing a set of words, and you must return the number of subsets of words that are prefix-free. Note that both the empty set and the entire set count as subsets.
[Official Solution]
This problem had a pretty simple theoretical solution that lead to a lot of different implementations. The basic idea was to insert every word into a trie. Then you would simply have a tree in which some nodes are marked (the ones in which a word ends). For those, you need to find the number of subsets S such that no node in S has a predecessor in S (note that in a trie predecessor nodes represent prefixes). This can be done recursively. For a node, the number of subsets on the subtree that starts on it is simply the product of the same function on all its childs, because they are all prefix independent. If a node is marked (i.e., represents a word in words) then add 1 to that product to represent the subset that contains that word (since the word represented is a prefix of all its childs, there is no way to combine those). The actual implementation varied a lot from one coder to the other. As a trick, it was good to notice that in a sorted (in lexicographic order) array, all prefixes appear before their predecessors. Also, if the subsets are seen as subsequences of the sorted sequence, each subset only needs to check consecutive elements to see if it is prefix-free (think for a while on why this happens). This leads to a really short dynammic programming solution.