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.
1. Strings are an array of characters.
2. Sentences are an array of words.
1. an array of
2. sentences are strings
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.
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.
1. Sets are a
2. Searching in
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:
Phrase1: 1 2 Phrase2: NONE
- How to get the first word of a sentence in PHP?
- Python | Remove all duplicates words from a given sentence
- Find minimum difference between any two elements | Set 2
- Kotlin constructor
- Parameterless Method in Scala
- How to xdebug var_dump to display full object/array ?
- How to create singleton design pattern in PHP 5 ?
- AngularJS | Filters
- AngularJS | ng-model-options Directive
- Introduction to Sockets.IO in Node
- GRE Data Analysis | Numerical Methods for Describing Data
- agetty command in Linux with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.