You can create a search data structure
Map<String,List<Finder>>
With a Finder
having the words int count
and max
and a res
. Please note that this list contains information that many sets in setToObjMap
can use the same word that is missing in your examples.
"telephone" -> [{res:"book",count=0,max=2}] "hat" -> same object as above "laugh" -> [{res:"house",count=0,max=3}] ...
This search collection is quickly built and even faster dumped after a search.
The search algorithm iterates through set
, for each word and each Finder for that word, it increments the count
variable. Second pass, take all the values ββof the search map, if count==max
, put res
in the result.
Init Algorithm:
for Entry e in setToObjMap Finder f = new Finder(e.value, 0, e.key.size) // res, count, max for String word in e.key lookup.get(word).add(f)
Search Algorithm:
for String word in set for Finder f in lookup.get(word) f.count ++ for Finder f in lookup.values() if (f.count==f.max) res.add(f.res)
Reset Algorithm:
for Finder f in lookup.values() f.count = 0
As for complexity, if n is the number of elements in set
and m is the number of values ββin setToObjMap
, then the complexity will be O (n + m)
source share