Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array | Function to count new pairs of strings that can be obtained by swapping first characters of any pair of strings ; Stores the count of pairs ; Generate all possible pairs of strings from the array arr [ ] ; Stores the current pair of strings ; Swap the first characters ; Check if they are already present in the array or not ; If both the strings are not present ; Increment the ans by 1 ; Print the resultant count ; Driver Code
def countStringPairs ( a , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT p = a [ i ] NEW_LINE q = a [ j ] NEW_LINE if ( p [ 0 ] != q [ 0 ] ) : NEW_LINE INDENT p = list ( p ) NEW_LINE q = list ( q ) NEW_LINE temp = p [ 0 ] NEW_LINE p [ 0 ] = q [ 0 ] NEW_LINE q [ 0 ] = temp NEW_LINE p = ' ' . join ( p ) NEW_LINE q = ' ' . join ( q ) NEW_LINE flag1 = 0 NEW_LINE flag2 = 0 NEW_LINE for k in range ( n ) : NEW_LINE INDENT if ( a [ k ] == p ) : NEW_LINE INDENT flag1 = 1 NEW_LINE DEDENT if ( a [ k ] == q ) : NEW_LINE INDENT flag2 = 1 NEW_LINE DEDENT DEDENT if ( flag1 == 0 and flag2 == 0 ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " good " , " bad " , " food " ] NEW_LINE N = len ( arr ) NEW_LINE countStringPairs ( arr , N ) NEW_LINE DEDENT