Ones and Zeroes - Problem
You are given an array of binary strings strs and two integers m and n.
Return the size of the largest subset of strs such that there are at most m zeros ('0's) and n ones ('1's) in the subset.
A set x is a subset of a set y if all elements of x are also elements of y.
Input & Output
Example 1 — Basic Case
$
Input:
strs = ["10","0001","111001","1","0"], m = 5, n = 3
›
Output:
4
💡 Note:
We can select at most 4 strings: "10" (1 zero, 1 one), "0001" (3 zeros, 1 one), "1" (0 zeros, 1 one), "0" (1 zero, 0 ones). Total: 5 zeros, 3 ones, which fits exactly within our limits.
Example 2 — Tight Constraints
$
Input:
strs = ["10","0","1"], m = 1, n = 1
›
Output:
2
💡 Note:
With only 1 zero and 1 one allowed, we can take either "10" (1 zero, 1 one) for count=1, OR "0" + "1" (1 zero, 1 one total) for count=2. The second option is better.
Example 3 — Edge Case
$
Input:
strs = ["111","1000"], m = 3, n = 1
›
Output:
1
💡 Note:
"111" has 3 ones but we only have budget for 1 one. "1000" has 3 zeros and 1 one, fits our constraints. Answer is 1.
Constraints
- 1 ≤ strs.length ≤ 600
- 1 ≤ strs[i].length ≤ 100
- strs[i] consists only of digits '0' and '1'
- 1 ≤ m, n ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of binary strings with zero/one budget limits
2
Count Characters
Each string consumes specific zeros and ones
3
Maximize Subset
Find largest subset fitting within budget
Key Takeaway
🎯 Key Insight: This is a 2D knapsack where each item has two costs and we maximize item count
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code