Count of times second string can be formed from the characters of first string | C ++ implementation of the approach ; Function to update the freq [ ] array to store the frequencies of all the characters of str ; Update the frequency of the characters ; Function to return the maximum count of times patt can be formed using the characters of str ; To store the frequencies of all the characters of str ; To store the frequencies of all the characters of patt ; To store the result ; For every character ; If the current character doesn 't appear in patt ; Update the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 26 ; void updateFreq ( string str , int freq [ ] ) { int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } } int maxCount ( string str , string patt ) { int strFreq [ MAX ] = { 0 } ; updateFreq ( str , strFreq ) ; int pattFreq [ MAX ] = { 0 } ; updateFreq ( patt , pattFreq ) ; int ans = INT_MAX ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( pattFreq [ i ] == 0 ) continue ; ans = min ( ans , strFreq [ i ] / pattFreq [ i ] ) ; } return ans ; } int main ( ) { string str = " geeksforgeeks " ; string patt = " geeks " ; cout << maxCount ( str , patt ) ; return 0 ; }