Count of strings in the first array which are smaller than every string in the second array | C ++ implementation of the approach ; Function to count the number of smaller strings in A [ ] for every string in B [ ] ; Count the frequency of all characters ; Iterate for all possible strings in A [ ] ; Increase the frequency of every character ; Check for the smallest character 's frequency ; Get the smallest character frequency ; Insert it in the vector ; Sort the count of all the frequency of the smallest character in every string ; Iterate for every string in B [ ] ; Hash set every frequency 0 ; Count the frequency of every character ; Find the frequency of the smallest character ; Count the number of strings in A [ ] which has the frequency of the smaller character less than the frequency of the smaller character of the string in B [ ] ; Store the answer ; Function to print the answer ; Get the answer ; Print the number of strings for every answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  26 NEW_LINE vector < int > findCount ( string a [ ] , string b [ ] , int n , int m ) { int freq [ MAX ] = { 0 } ; vector < int > smallestFreq ; for ( int i = 0 ; i < n ; i ++ ) { string s = a [ i ] ; memset ( freq , 0 , sizeof freq ) ; for ( int j = 0 ; j < s . size ( ) ; j ++ ) { freq [ s [ j ] - ' a ' ] ++ ; } for ( int j = 0 ; j < MAX ; j ++ ) { if ( freq [ j ] ) { smallestFreq . push_back ( freq [ j ] ) ; break ; } } } sort ( smallestFreq . begin ( ) , smallestFreq . end ( ) ) ; vector < int > ans ; for ( int i = 0 ; i < m ; i ++ ) { string s = b [ i ] ; memset ( freq , 0 , sizeof freq ) ; for ( int j = 0 ; j < s . size ( ) ; j ++ ) { freq [ s [ j ] - ' a ' ] ++ ; } int frequency = 0 ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( freq [ j ] ) { frequency = freq [ j ] ; break ; } } int ind = lower_bound ( smallestFreq . begin ( ) , smallestFreq . end ( ) , frequency ) - smallestFreq . begin ( ) ; ans . push_back ( ind ) ; } return ans ; } void printAnswer ( string a [ ] , string b [ ] , int n , int m ) { vector < int > ans = findCount ( a , b , n , m ) ; for ( auto it : ans ) { cout << it << " ▁ " ; } } int main ( ) { string A [ ] = { " aaa " , " aa " , " bdc " } ; string B [ ] = { " cccch " , " cccd " } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int m = sizeof ( B ) / sizeof ( B [ 0 ] ) ; printAnswer ( A , B , n , m ) ; return 0 ; }