I have a list that contains k lists of strings (each of these k lists do not have any duplicate string). We know the union of all possible strings (suppose we have n unique strings).
What we need to find is: What is the most frequent pair of strings (i.e., which 2 strings appear together the most across the k lists? And the second most frequent pair of strings, the third most frequent pair of strings, etc. Also, I'd like to know the most frequent triplet of strings, the second most frequent triplet of strings, etc.
The only algorithm that I could think of to do this is of terrible complexity, where basically to solve for the most frequent pair, I'd enumerate all possible pairs out of the n strings (O(n^2)) and for each of them check how many lists have them (O(k)) and then I'll sort the results to get what I need, and so my overall complexity is O(n^2.x), ignoring the last sort.
Any ideas for a better algorithm time-wise? (that would hopefully work well for triplets of strings and quadruplets of strings, etc)? Code in python is best, but detailed pseudocode (and data structure, if relevant) or detailed general idea is fine, too!
For example: If
myList=[['AB', 'AC', 'ACC'], ['AB','ACC'],['ACC'],['AC','ACC'],['ACC','BB','AC']],
Then the expected output of the pairs question would be: 'AC','ACC' is the most frequent pair and 'AB','ACC' is the second most frequent pair.