Maximum number of times str1 appears as a non | Javascript implementation of the approach ; Function to return the maximum number of times str1 can appear as a non - overlapping substring in str2 ; str1 cannot never be substring of str2 ; Store the frequency of the characters of str1 ; Store the frequency of the characters of str2 ; To store the required count of substrings ; Current character doesn 't appear in str1 ; Frequency of the current character in str1 is greater than its frequency in str2 ; Update the count of possible substrings ; Driver code
const MAX = 26 ; function maxSubStr ( str1 , len1 , str2 , len2 ) { if ( len1 > len2 ) return 0 ; let freq1 = new Array ( MAX ) . fill ( 0 ) ; for ( let i = 0 ; i < len1 ; i ++ ) freq1 [ str1 . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ] ++ ; let freq2 = new Array ( MAX ) . fill ( 0 ) ; for ( let i = 0 ; i < len2 ; i ++ ) freq2 [ str2 . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ] ++ ; let minPoss = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < MAX ; i ++ ) { if ( freq1 [ i ] == 0 ) continue ; if ( freq1 [ i ] > freq2 [ i ] ) return 0 ; minPoss = Math . min ( minPoss , Math . floor ( freq2 [ i ] / freq1 [ i ] ) ) ; } return minPoss ; } let str1 = " " , str2 = " " ; let len1 = str1 . length ; let len2 = str2 . length ; document . write ( maxSubStr ( str1 , len1 , str2 , len2 ) ) ;