Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array | Python3 program to implement the above approach ; Function to count the distinct strings possible after swapping the prefixes between two possible strings of the array ; Stores the count of unique characters for each index ; Store current string ; Stores the total number of distinct strings possible ; Return the answer ; Driver Code
from collections import defaultdict NEW_LINE mod = 1000000007 NEW_LINE def countS ( string : list , n : int , m : int ) -> int : NEW_LINE INDENT counts = defaultdict ( lambda : set ( ) ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = string [ i ] NEW_LINE for j in range ( m ) : NEW_LINE INDENT counts [ j ] . add ( s [ j ] ) NEW_LINE DEDENT DEDENT result = 1 NEW_LINE for index in counts : NEW_LINE INDENT result = ( result * len ( counts [ index ] ) ) % mod NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = [ "112" , "211" ] NEW_LINE N = 2 NEW_LINE M = 3 NEW_LINE print ( countS ( string , N , M ) ) NEW_LINE DEDENT