Count of strings in the first array which are smaller than every string in the second array | Python3 implementation of the approach ; Function to count the number of smaller strings in A for every in B ; Count the frequency of all characters ; Iterate for all possible strings in A ; Increase the frequency of every character ; Check for the smallest character 's frequency ; Get the smallest character frequency ; Insert it in the vector ; Sort the count of all the frequency of the smallest character in every string ; Iterate for every in B ; Hash set every frequency 0 ; Count the frequency of every character ; Find the frequency of the smallest character ; Count the number of strings in A which has the frequency of the smaller character less than the frequency of the smaller character of the in B ; Store the answer ; Function to print the answer ; Get the answer ; Print the number of strings for every answer ; Driver code
from bisect import bisect_left as lower_bound NEW_LINE MAX = 26 NEW_LINE def findCount ( a , b , n , m ) : NEW_LINE INDENT freq = [ 0 for i in range ( MAX ) ] NEW_LINE smallestFreq = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = a [ i ] NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT freq [ i ] = 0 NEW_LINE DEDENT for j in range ( len ( s ) ) : NEW_LINE INDENT freq [ ord ( s [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for j in range ( MAX ) : NEW_LINE INDENT if ( freq [ j ] ) : NEW_LINE INDENT smallestFreq . append ( freq [ j ] ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT smallestFreq = sorted ( smallestFreq ) NEW_LINE ans = [ ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT s = b [ i ] NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT freq [ i ] = 0 NEW_LINE DEDENT for j in range ( len ( s ) ) : NEW_LINE INDENT freq [ ord ( s [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT frequency = 0 NEW_LINE for j in range ( MAX ) : NEW_LINE INDENT if ( freq [ j ] ) : NEW_LINE INDENT frequency = freq [ j ] NEW_LINE break NEW_LINE DEDENT DEDENT ind = lower_bound ( smallestFreq , frequency ) NEW_LINE ans . append ( ind ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def printAnswer ( a , b , n , m ) : NEW_LINE INDENT ans = findCount ( a , b , n , m ) NEW_LINE for it in ans : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT A = [ " aaa " , " aa " , " bdc " ] NEW_LINE B = [ " cccch " , " cccd " ] NEW_LINE n = len ( A ) NEW_LINE m = len ( B ) NEW_LINE printAnswer ( A , B , n , m ) NEW_LINE