Pairs of complete strings in two sets of strings | Returns count of complete pairs from set [ 0. . n - 1 ] and set2 [ 0. . m - 1 ] ; con_s1 [ i ] is going to store an integer whose set bits represent presence / absence of characters in string set1 [ i ] . Similarly con_s2 [ i ] is going to store an integer whose set bits represent presence / absence of characters in string set2 [ i ] ; Process all strings in set1 [ ] ; initializing all bits to 0 ; Setting the ascii code of char s [ i ] [ j ] to 1 in the compressed integer . ; Process all strings in set2 [ ] ; initializing all bits to 0 ; setting the ascii code of char s [ i ] [ j ] to 1 in the compressed integer . ; assigning a variable whose all 26 ( 0. . 25 ) bits are set to 1 ; Now consider every pair of integer in con_s1 [ ] and con_s2 [ ] and check if the pair is complete . ; if all bits are set , the strings are complete ! ; Driver code
function countCompletePairs ( set1 , set2 , n , m ) { let result = 0 ; let con_s1 = new Array ( n ) ; let con_s2 = new Array ( m ) ; for ( let i = 0 ; i < n ; i ++ ) { con_s1 [ i ] = 0 ; for ( let j = 0 ; j < set1 [ i ] . length ; j ++ ) { con_s1 [ i ] = con_s1 [ i ] | ( 1 << ( set1 [ i ] [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ; } } for ( let i = 0 ; i < m ; i ++ ) { con_s2 [ i ] = 0 ; for ( let j = 0 ; j < set2 [ i ] . length ; j ++ ) { con_s2 [ i ] = con_s2 [ i ] | ( 1 << ( set2 [ i ] [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ; } } let complete = ( 1 << 26 ) - 1 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( ( con_s1 [ i ] con_s2 [ j ] ) == complete ) { result ++ ; } } } return result ; } let set1 = [ " " , " " , " " , " " ] ; let set2 = [ " " , " " , " " ] let n = set1 . length ; let m = set2 . length ; document . write ( countCompletePairs ( set1 , set2 , n , m ) ) ;