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
function countStringPairs ( a , n ) { let ans = 0 ; let s = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! s . has ( a [ i ] ) ) s . set ( a [ i ] , 0 ) ; s . set ( a [ i ] , s . get ( a [ i ] ) + 1 ) ; } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { let p = ( a [ i ] ) . split ( " " ) ; let q = ( a [ j ] ) . split ( " " ) ; if ( p [ 0 ] != q [ 0 ] ) { let t = p [ 0 ] ; p [ 0 ] = q [ 0 ] ; q [ 0 ] = t ; if ( ! s . has ( p . join ( " " ) ) && ! s . has ( q . join ( " " ) ) ) { ans ++ ; } } } } document . write ( ans ) ; } let arr = [ " " , " " , " " ] ; let N = arr . length ; countStringPairs ( arr , N ) ;