Total character pairs from two strings , with equal number of set bits in their ascii value | C # implementation of the approach ; Function to return the count of valid pairs ; Store frequency of number of set bits for s1 ; Store frequency of number of set bits for s2 ; Calculate total pairs ; Return the count of valid pairs ; Driver code
using System ; using System . Linq ; class GFG { static int totalPairs ( string s1 , string s2 ) { int count = 0 ; int [ ] arr1 = new int [ 7 ] ; int [ ] arr2 = new int [ 7 ] ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { int set_bits = Convert . ToString ( ( int ) s1 [ i ] , 2 ) . Count ( c => c == '1' ) ; arr1 [ set_bits ] ++ ; } for ( int i = 0 ; i < s2 . Length ; i ++ ) { int set_bits = Convert . ToString ( ( int ) s2 [ i ] , 2 ) . Count ( c => c == '1' ) ; arr2 [ set_bits ] ++ ; } for ( int i = 1 ; i <= 6 ; i ++ ) count += ( arr1 [ i ] * arr2 [ i ] ) ; return count ; } static void Main ( ) { string s1 = " geeks " ; string s2 = " forgeeks " ; Console . WriteLine ( totalPairs ( s1 , s2 ) ) ; } }