Most frequent word in first String which is not present in second String | CPP implementation of above approach ; Function to return frequent word from S1 that isn 't present in S2 ; create map of banned words ; find smallest and most frequent word ; check if word is not banned ; return answer ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string smallestFreq ( string S1 , string S2 ) { map < string , int > banned ; for ( int i = 0 ; i < S2 . length ( ) ; ++ i ) { string s = " " ; while ( i < S2 . length ( ) && S2 [ i ] != ' ▁ ' ) s += S2 [ i ++ ] ; banned [ s ] ++ ; } map < string , int > result ; string ans ; int freq = 0 ; for ( int i = 0 ; i < S1 . length ( ) ; ++ i ) { string s = " " ; while ( i < S1 . length ( ) && S1 [ i ] != ' ▁ ' ) s += S1 [ i ++ ] ; if ( banned [ s ] == 0 ) { result [ s ] ++ ; if ( result [ s ] > freq || ( result [ s ] == freq && s < ans ) ) { ans = s ; freq = result [ s ] ; } } } return ans ; } int main ( ) { string S1 = " geeks ▁ for ▁ geeks ▁ is ▁ best ▁ place ▁ to ▁ learn " ; string S2 = " bad ▁ place " ; cout << smallestFreq ( S1 , S2 ) ; return 0 ; }