Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array | Function to count newly created pairs by swapping the first characters of any pairs of strings ; Stores the count all possible pair of strings ; Push all the strings into the Unordered Map ; Generate all possible pairs of strings from the array arr [ ] ; Store the current pair of strings ; Swap the first character ; Check if both string are not present in map ; Print the result ; Driver Code
def countStringPairs ( a , n ) : NEW_LINE INDENT ans = 0 NEW_LINE s = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT s [ a [ i ] ] = s . get ( a [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT p = [ i for i in a [ i ] ] NEW_LINE q = [ j for j in a [ j ] ] NEW_LINE if ( p [ 0 ] != q [ 0 ] ) : NEW_LINE INDENT p [ 0 ] , q [ 0 ] = q [ 0 ] , p [ 0 ] NEW_LINE if ( ( " " . join ( p ) not in s ) and ( " " . join ( q ) not in s ) ) : NEW_LINE INDENT 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