Count of times second string can be formed from the characters of first string | JavaScript 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
const MAX = 26 ; function updateFreq ( str , freq ) { var len = str . length ; for ( var i = 0 ; i < len ; i ++ ) { freq [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } } function maxCount ( str , patt ) { var strFreq = new Array ( MAX ) . fill ( 0 ) ; updateFreq ( str , strFreq ) ; var pattFreq = new Array ( MAX ) . fill ( 0 ) ; updateFreq ( patt , pattFreq ) ; var ans = 21474836473 ; for ( var i = 0 ; i < MAX ; i ++ ) { if ( pattFreq [ i ] == 0 ) continue ; ans = Math . min ( ans , strFreq [ i ] / pattFreq [ i ] ) ; } return ans ; } var str = " " ; var patt = " " ; document . write ( maxCount ( str , patt ) ) ;