Count of words that are present in all the given sentences in C++


We are given multiple sentences in the form of strings. The goal is to count the number of words that exist in all of the sentences.

Note − words containing all lowercase letters will be considered only

If sentences are −

“ I am learning C language ”

“ learning new things is easy “

“ Kids are learning healthy habits “

Only “learning” exists in all three. So count is 1.

Let us understand with examples

Input − “The clothes were dry”, “All the kids were playing”, “Those were the best days”

Output − Count of words that are present in all the given sentences are − 2

Explanation − Words “the” and “were” are present in all sentences.

Input − “We are going to school”, “If you are willing then continue”, “All these are sold”

Output − Count of words that are present in all the given sentences are − 1

Explanation − Word “are” is present in all sentences.

Approach used in the below program is as follows

In this approach we will first store the words of the first sentence in vector<pair<string, bool>> set.

We will find words in the vector set in all other sentences using a unordered_map<string, bool> check.

  • Take a vector<string> vec. And initialize it with all strings containing sentences.

  • Number of sentences will be vec.size().

  • Function words_sentences(vector<string> vec, int size) takes vector of sentences and size and returns the count of words that are present in all the given sentences

  • Take the initial count as 0.

  • Take temporary string str to store individual words in a sentence.

  • Traverse first sentence stored in vec[0] using while loop.

  • Inside it using another while loop, extract an individual word in str[] till space is encountered.

  • Now that we have a word of the first sentence in str, add a pair of (str,true) to the set.

  • Do this for all words in the sentence stored in vec[0].

  • The vector set now has pairs of all words of the first sentence with true value.

  • Traverse vector of sentences from 2nd sentence to last sentence using for loop from j=1 to j<size.

  • Extract each word from the current sentence in vec[j] and store in str.

  • Mark these words as true in map check using check[str]=true.

  • Do this for all words in the current sentence in vec[j].

  • Using for loop, traverse vector set and for current sentence find if these words in check are also in the set.

  • Using for loop, traverse vector set again.

  • If the current word appears in all sentences then set[k].second will be true. If yes increment count.

  • At the end we will have variable count with words occurring in all sentences.

  • Return count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int words_sentences(vector<string> vec, int size){
   int count = 0;
   int i = 0;
   string str;
   unordered_map<string, bool> check;
   vector<pair<string, bool>> set ;
   pair<string, bool> str_bool;
   while (i < vec[0].size()){
      str = "";
      while (i < vec[0].size() && vec[0][i] != ' '){
         str += vec[0][i];
         i++;
      }
      i++;
      if (str != ""){
         str_bool = make_pair(str, true);
         set.push_back(str_bool);
      }
   }
   for (int j = 1; j < size; j++){
      check.clear();
      i = 0;
      while (i < vec[j].size()){
         str = "";
         while (i < vec[j].size() && vec[j][i] != ' '){
            str += vec[j][i];
            i++;
         }
         i++;
         if (str != ""){
            check[str] = true;
         }
      }
      for(int k = 0; k < set.size(); k++){
         if (set[k].second != false && check[set[k].first] == false){
            set[k].second = false;
         }
         else if (set[k].second != false && check[set[k].first] == true){
            check[set[k].first] = false;
         }
      }
   }
   for (int k = 0; k < set.size(); k++){
      if (set[k].second == true){
         count++;
      }
   }
   return count;
}
int main(){
   vector<string> vec;
   vec.push_back("Honesty is the best policy");
   vec.push_back("policy varies from company to company");
   vec.push_back("Employee should follow the policy of a company");
   int size = vec.size();
   cout<<"Count of words that are present in all the given sentences are: "<<words_sentences(vec, size);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of words that are present in all the given sentences are: 1

Updated on: 03-Dec-2020

156 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements