Skip to content
Discussion options

You must be logged in to vote

We need to find the largest subset of binary strings where the total count of 0's is ≤ m and total count of 1's is ≤ n.

This is essentially a 2D knapsack problem where each string has a "weight" in terms of zeros and ones, and I need to maximize the number of items (strings) I can select without exceeding capacity m for zeros and n for ones.

Approach

I'll use dynamic programming with a 2D DP array:

  • dp[i][j] = maximum number of strings that can be selected with at most i zeros and j ones
  • For each string, I'll count its zeros and ones, then update the DP table

Let's implement this solution in PHP: 474. Ones and Zeroes

<?php
/**
 * @param String[] $strs
 * @param Integer $m
 * @param Integ…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Nov 11, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Nov 11, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants