Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; public class GFG { static void countStringPairs ( string [ ] a , int n ) { int ans = 0 ; Dictionary < string , int > s = new Dictionary < string , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! s . ContainsKey ( a [ i ] ) ) s . Add ( a [ i ] , 0 ) ; s [ a [ i ] ] ++ ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { char [ ] p = ( a [ i ] ) . ToCharArray ( ) ; char [ ] q = ( a [ j ] ) . ToCharArray ( ) ; if ( p [ 0 ] != q [ 0 ] ) { char t = p [ 0 ] ; p [ 0 ] = q [ 0 ] ; q [ 0 ] = t ; if ( ! s . ContainsKey ( new string ( p ) ) && ! s . ContainsKey ( new string ( q ) ) ) { ans ++ ; } } } } Console . WriteLine ( ans ) ; } static public void Main ( ) { string [ ] arr = { " good " , " bad " , " food " } ; int N = arr . Length ; countStringPairs ( arr , N ) ; } }