text: Number of strings in two array satisfy the given conditions | C # 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: using System ; using System . Collections . Generic ; class GFG { static void findNumOfValidWords ( List < String > w , List < String > p ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; List < int > res = new List < int > ( ) ; foreach ( String s in w ) { int val = 0 ; foreach ( char c in s . ToCharArray ( ) ) { val = val | ( 1 << ( c - ' a ' ) ) ; } if ( m . ContainsKey ( val ) ) m [ val ] = m [ val ] + 1 ; else m . Add ( val , 1 ) ; } foreach ( String s in p ) { int val = 0 ; foreach ( char c in s . ToCharArray ( ) ) { val = val | ( 1 << ( c - ' a ' ) ) ; } int temp = val ; int first = s [ 0 ] - ' a ' ; int count = 0 ; while ( temp != 0 ) { if ( ( ( temp >> first ) & 1 ) == 1 ) { if ( m . ContainsKey ( temp ) ) { count += m [ temp ] ; } } temp = ( temp - 1 ) & val ; } res . Add ( count ) ; } foreach ( int it in res ) { Console . WriteLine ( it ) ; } } public static void Main ( String [ ] args ) { List < String > arr1 = new List < String > ( ) ; arr1 . Add ( " aaaa " ) ; arr1 . Add ( " asas " ) ; arr1 . Add ( " able " ) ; arr1 . Add ( " ability " ) ; arr1 . Add ( " actt " ) ; arr1 . Add ( " actor " ) ; arr1 . Add ( " access " ) ; List < String > arr2 = new List < String > ( ) ; arr2 . Add ( " aboveyz " ) ; arr2 . Add ( " abrodyz " ) ; arr2 . Add ( " absolute " ) ; arr2 . Add ( " absoryz " ) ; arr2 . Add ( " actresz " ) ; arr2 . Add ( " gaswxyz " ) ; findNumOfValidWords ( arr1 , arr2 ) ; } }