text: Number of strings in two array satisfy the given conditions | Java 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
code: import java . util . * ; class GFG { static void findNumOfValidWords ( Vector < String > w , Vector < String > p ) { HashMap < Integer , Integer > m = new HashMap < > ( ) ; Vector < Integer > res = new Vector < > ( ) ; for ( String s : w ) { int val = 0 ; for ( char c : s . toCharArray ( ) ) { val = val | ( 1 << ( c - ' a ' ) ) ; } if ( m . containsKey ( val ) ) m . put ( val , m . get ( val ) + 1 ) ; else m . put ( val , 1 ) ; } for ( String s : p ) { int val = 0 ; for ( char c : s . toCharArray ( ) ) { val = val | ( 1 << ( c - ' a ' ) ) ; } int temp = val ; int first = s . charAt ( 0 ) - ' a ' ; int count = 0 ; while ( temp != 0 ) { if ( ( ( temp >> first ) & 1 ) == 1 ) { if ( m . containsKey ( temp ) ) { count += m . get ( temp ) ; } } temp = ( temp - 1 ) & val ; } res . add ( count ) ; } for ( int it : res ) { System . out . println ( it ) ; } } public static void main ( String [ ] args ) { Vector < String > arr1 = new Vector < > ( ) ; arr1 . add ( " aaaa " ) ; arr1 . add ( " asas " ) ; arr1 . add ( " able " ) ; arr1 . add ( " ability " ) ; arr1 . add ( " actt " ) ; arr1 . add ( " actor " ) ; arr1 . add ( " access " ) ; Vector < String > arr2 = new Vector < > ( ) ; arr2 . add ( " aboveyz " ) ; arr2 . add ( " abrodyz " ) ; arr2 . add ( " absolute " ) ; arr2 . add ( " absoryz " ) ; arr2 . add ( " actresz " ) ; arr2 . add ( " gaswxyz " ) ; findNumOfValidWords ( arr1 , arr2 ) ; } }