Number of strings in two array satisfy the given conditions | Javascript program for the above approach ; To store the frequency of string after bitmasking ; To store result for each string in arr2 [ ] ; Traverse the arr1 [ ] and bitmask each string in it ; Bitmasking for each string s ; Update the frequency of string with it 's bitmasking value ; Traverse the arr2 [ ] ; Bitmasking for each String s ; Check if temp is present in an unordered_map or not ; Check for next set bit ; Push the count for current string in resultant array ; Print the count for each string ; Driver Code ; Function call
function findNumOfValidWords ( w , p ) { var m = new Map ( ) ; var res = [ ] ; w . forEach ( s => { var val = 0 ; s . split ( ' ' ) . forEach ( c => { val = val | ( 1 << ( c . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ; } ) ; if ( m . has ( val ) ) m . set ( val , m . get ( val ) + 1 ) else m . set ( val , 1 ) } ) ; p . forEach ( s => { var val = 0 ; s . split ( ' ' ) . forEach ( c => { val = val | ( 1 << ( c . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ; } ) ; var temp = val ; var first = s [ 0 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; var count = 0 ; while ( temp != 0 ) { if ( ( ( temp >> first ) & 1 ) == 1 ) { if ( m . has ( temp ) ) { count += m . get ( temp ) ; } } temp = ( temp - 1 ) & val ; } res . push ( count ) ; } ) ; res . forEach ( it => { document . write ( it + " " ) ; } ) ; } var arr1 = [ " " , " " , " " , " " , " " , " " , " " ] ; var arr2 = [ " " , " " , " " , " " , " " , " " ] ; findNumOfValidWords ( arr1 , arr2 ) ;