Count of pairs of strings which differ in exactly one position | Function to return the count of same pairs ; Function to return total number of strings which satisfy required condition ; Dictionary changed will store strings with wild cards Dictionary same will store strings that are equal ; Iterating for all strings in the given array ; If we found the string then increment by 1 Else it will get default value 0 ; Iterating on a single string ; Incrementing the string if found Else it will get default value 0 ; Return counted pairs - equal pairs ; Driver code
def pair_count ( d ) : NEW_LINE INDENT return sum ( ( i * ( i - 1 ) ) // 2 for i in d . values ( ) ) NEW_LINE DEDENT def Difference ( array , m ) : NEW_LINE INDENT changed , same = { } , { } NEW_LINE for s in array : NEW_LINE INDENT same [ s ] = same . get ( s , 0 ) + 1 NEW_LINE for i in range ( m ) : NEW_LINE INDENT changed [ t ] = changed . get ( t , 0 ) + 1 NEW_LINE DEDENT DEDENT return pair_count ( changed ) - pair_count ( same ) * m NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , m = 3 , 3 NEW_LINE array = [ " abc " , " abd " , " bbd " ] NEW_LINE print ( Difference ( array , m ) ) NEW_LINE DEDENT