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
function countStringPairs ( a , n ) { var ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { var p = a [ i ] ; var q = a [ j ] ; if ( p [ 0 ] !== q [ 0 ] ) { p = p . split ( " " ) ; q = q . split ( " " ) ; var temp = p [ 0 ] ; p [ 0 ] = q [ 0 ] ; q [ 0 ] = temp ; p = p . join ( " " ) ; q = q . join ( " " ) ; var flag1 = 0 ; var flag2 = 0 ; for ( let k = 0 ; k < n ; k ++ ) { if ( a [ k ] === p ) flag1 = 1 ; if ( a [ k ] === q ) flag2 = 1 ; } if ( flag1 === 0 && flag2 === 0 ) { ans = ans + 1 ; } } } } document . write ( ans ) ; } var arr = [ " " , " " , " " ] ; var N = arr . length ; countStringPairs ( arr , N ) ;