Print all words occurring in a sentence exactly K times | CPP program for the above approach ; Function to print all the words occurring k times in a string ; Stores the words ; Traverse the list ; Check for count ; Print the word ; Remove from list ; Driver Code ; Given string ; Given value of K ; Function call to find all words occurring K times
#include <bits/stdc++.h> NEW_LINE using namespace std ; void kFreqWords ( string S , int K ) { string temp = " " ; vector < string > l ; for ( auto x : S ) { if ( x == ' ▁ ' ) { l . push_back ( temp ) ; temp = " " ; } else temp += x ; } for ( auto x : l ) { if ( count ( l . begin ( ) , l . end ( ) , x ) == K ) { cout << x << endl ; remove ( l . begin ( ) , l . end ( ) , x ) ; } } } int main ( ) { string S = " banana ▁ is ▁ in ▁ yellow ▁ and ▁ sun ▁ flower ▁ is ▁ also ▁ in ▁ yellow ▁ " ; int K = 2 ; kFreqWords ( S , K ) ; }