Count of strings that can be formed from another string using each character at | 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
< ? php function findNumberOfTimes ( $ str1 , $ str2 ) { $ freq = array_fill ( 0 , 26 , NULL ) ; $ l1 = strlen ( $ str1 ) ; $ freq2 = array_fill ( 0 , 26 , NULL ) ; $ l2 = strlen ( $ str2 ) ; for ( $ i = 0 ; $ i < $ l1 ; $ i ++ ) $ freq [ ord ( $ str1 [ $ i ] ) - ord ( ' a ' ) ] += 1 ; for ( $ i = 0 ; $ i < $ l2 ; $ i ++ ) $ freq2 [ ord ( $ str2 [ $ i ] ) - ord ( ' a ' ) ] += 1 ; $ count = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ l2 ; $ i ++ ) $ count = min ( $ count , $ freq [ ord ( $ str2 [ $ i ] ) - ord ( ' a ' ) ] / $ freq2 [ ord ( $ str2 [ $ i ] ) - ord ( ' a ' ) ] ) ; return $ count ; } $ str1 = " foreeksgekseg " ; $ str2 = " geeks " ; echo findNumberOfTimes ( $ str1 , $ str2 ) . " " ; ? >