I am trying to optimize a program, where I need to know whether a given set of keywords present in the set of words. I believe using the dictionary is the only way to optimize it. Any other technique so that I can get better time complexity? The below python script is my version of the code, this works for a small sentence with a time complexity
O(len(keywords)) + O(frequency_table CONSTRUCTION TIME).
Space complexity is not a problem, but time.
def cover_test(keys, sentence): frequency_table = collections.Counter(sentence) count = 0 for key in keys: if key in frequency_table: count += 1 return count == len(keys) cover_test("word1 word4".split(), "word2 word1 word3 word4".split()) # output # True, because word1 and word4 is present in the sentense
examples of input
Set of words
['apple', 'green', 'horse', 'ink', 'ball', 'cat', 'dog', 'elephant', 'fun']
Set of keywords
Output for the above input is True
Update: More clarifications: The goal is to ensure that every word from the keys list is included in the sentence list, in linear or less time complexity.