Count of strings that can be formed from another string using each character at | / C ++ program to print the number of times str2 can be formed from str1 using the characters of str1 only once ; Function to find the number of str2 that can be formed using characters of str1 ; iterate and mark the frequencies of all characters in str1 ; find the minimum frequency of every character in str1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumberOfTimes ( string str1 , string str2 ) { int freq [ 26 ] = { 0 } ; int freq2 [ 26 ] = { 0 } ; int l1 = str1 . length ( ) ; int l2 = str2 . length ( ) ; for ( int i = 0 ; i < l1 ; i ++ ) freq [ str1 [ i ] - ' a ' ] += 1 ; for ( int i = 0 ; i < l2 ; i ++ ) freq2 [ str2 [ i ] - ' a ' ] += 1 ; int count = INT_MAX ; for ( int i = 0 ; i < l2 ; i ++ ) { if ( freq2 [ str2 [ i ] - ' a ' ] != 0 ) count = min ( count , freq [ str2 [ i ] - ' a ' ] / freq2 [ str2 [ i ] - ' a ' ] ) ; } return count ; } int main ( ) { string str1 = " foreeksgekseg " ; string str2 = " geeks " ; cout << findNumberOfTimes ( str1 , str2 ) << endl ; return 0 ; }