Count of strings that can be formed from another string using each character at | Python3 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
import sys NEW_LINE def findNumberOfTimes ( str1 , str2 ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE l1 = len ( str1 ) NEW_LINE freq2 = [ 0 ] * 26 NEW_LINE l2 = len ( str2 ) NEW_LINE for i in range ( l1 ) : NEW_LINE INDENT freq [ ord ( str1 [ i ] ) - ord ( " a " ) ] += 1 NEW_LINE DEDENT for i in range ( l2 ) : NEW_LINE INDENT freq2 [ ord ( str2 [ i ] ) - ord ( " a " ) ] += 1 NEW_LINE DEDENT count = sys . maxsize NEW_LINE for i in range ( l2 ) : NEW_LINE count = min ( count , freq [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] / freq2 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] ) NEW_LINE return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " foreeksgekseg " NEW_LINE str2 = " geeks " NEW_LINE print ( findNumberOfTimes ( str1 , str2 ) ) NEW_LINE DEDENT