text: Length of longest subset consisting of A 0 s and B 1 s from an array of strings | Set 2 | Function to find the length of the longest subset of an array of strings with at most A 0 s and B 1 s ; Initialize a 2D array with its entries as 0 ; Traverse the given array ; Store the count of 0 s and 1 s in the current string ; Iterate in the range [ A , zeros ] ; Iterate in the range [ B , ones ] ; Update the value of dp [ i ] [ j ] ; Print the result ; Driver Code
code: function MaxSubsetlength ( arr , A , B ) { var dp = Array . from ( Array ( A + 1 ) , ( ) => Array ( B + 1 ) . fill ( 0 ) ) ; arr . forEach ( str => { var zeros = [ ... str ] . filter ( x => x == ' ' ) . length ; var ones = [ ... str ] . filter ( x => x == ' ' ) . length ; for ( var i = A ; i >= zeros ; i -- ) for ( var j = B ; j >= ones ; j -- ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - zeros ] [ j - ones ] + 1 ) ; } ) ; return dp [ A ] [ B ] ; } var arr = [ " " , " " , " " , " " , " " ] ; var A = 5 , B = 3 ; document . write ( MaxSubsetlength ( arr , A , B ) ) ;