text: Total character pairs from two strings , with equal number of set bits in their ascii value | JavaScript implementation of the approach Function to get no of set bits in binary representation of positive integer n ; 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
code: function countSetBits ( n ) { var count = 0 ; while ( n ) { count += n & 1 ; n >>= 1 ; } return count ; } function totalPairs ( s1 , s2 ) { var count = 0 ; var arr1 = new Array ( 7 ) . fill ( 0 ) ; var arr2 = new Array ( 7 ) . fill ( 0 ) ; for ( let i = 0 ; i < s1 . length ; i ++ ) { set_bits = countSetBits ( s1 [ i ] . charCodeAt ( 0 ) ) ; arr1 [ set_bits ] += 1 ; } for ( let i = 0 ; i < s2 . length ; i ++ ) { set_bits = countSetBits ( s2 [ i ] . charCodeAt ( 0 ) ) ; arr2 [ set_bits ] += 1 ; } for ( let i = 1 ; i < 7 ; i ++ ) { count += arr1 [ i ] * arr2 [ i ] ; } return count ; } var s1 = " " ; var s2 = " " ; document . write ( totalPairs ( s1 , s2 ) ) ;