Given a list of sentences and a list of phrases. The task is to find which sentence(s) contain all the words in a phrase and for every phrase print the sentences number that contains the given phrase.
Constraint: A word cannot be a part of more than 10 sentences.
Examples:
Input:
Sentences:
1. Strings are an array of characters.
2. Sentences are an array of words.
Phrases:
1. an array of
2. sentences are strings
Output:
Phrase1 is present in sentences: 1,2
Phrase2 is present in sentences: None
Since each word in phrase 1 exists in both the sentences,
but all the words in second phrase do not exist in either.Input:
Sentences:
1. Sets in python are a hash table representation of arrays.
2. Searching in Sets are a function of time complexity O(1).
3. Sets only contain unique elements, and have no order.
Phrases:
1. Sets are a
2. Searching in
Output:
Phrase1 is present in sentences: 1, 2
Phrase2 is present in sentences: 2
Approach: For each Phrase, we have to find the sentences which contain all the words of the phrase. So, for each word in the given phrase, we check if a sentence contains it. We do this for each sentence. This process of searching may become faster if the words in the sentence are stored in a set instead of a list.
Below is the implementation of above approach:
# Python program to find the sentence # that contains all the given phrases def getRes(sent, ph): sentHash = dict() # Loop for adding hased sentences to sentHash for s in range(1, len(sent)+1): sentHash[s] = set(sent[s-1].split()) # For Each Phrase for p in range(0, len(ph)): print("Phrase"+str(p + 1)+":") # Get the list of Words wordList = ph[p].split() res = [] # Then Check in every Sentence for s in range(1, len(sentHash)+1): wCount = len(wordList) # Every word in the Phrase for w in wordList: if w in sentHash[s]: wCount -= 1 # If every word in phrase matches if wCount == 0: # add Sentence Index to result Array res.append(s) if(len(res) == 0): print("NONE") else: print('% s' % ' '.join(map(str, res))) # Driver Function def main(): sent = ["Strings are an array of characters", "Sentences are an array of words"] ph = ["an array of", "sentences are strings"] getRes(sent, ph) main() |
Phrase1: 1 2 Phrase2: NONE
Recommended Posts:
- Python | Remove all duplicates words from a given sentence
- How to get the first word of a sentence in PHP?
- Print all the pairs that contains the positive and negative values of an element
- Find if an undirected graph contains an independent set of a given size
- string.Contains Function in Golang with Examples
- ConcurrentLinkedDeque contains() method in Java with Examples
- HTML | DOM contains() Method
- How to check an element contains a class using jQuery?
- Python | Test if dictionary contains unique keys and values
- How to determine if an array contains a specific value in PHP?
- How to select html element using ID in jQuery when ID contains a dot character ?
- Print all nodes at distance k from a given node
- Program to print all distinct elements of a given integer array in Python | Ordered Dictionary
- Find all good indices in the given Array
- Python | Get the smallest window in a string containing all characters of given pattern
- How to select all elements without a given class using jQuery ?
- Count number of times each Edge appears in all possible paths of a given Tree
- Print all positions of a given string having count of smaller characters equal on both sides
- Print sublist of a given Linked List specified by given indices
- Print all possible words from phone digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

