Ones and Zeroes - Problem

You're building the ultimate collection of binary strings, but there's a catch - you have limited resources! Given an array of binary strings and two constraints (maximum m zeros and n ones), find the largest possible subset you can create.

The Challenge: Each binary string costs a certain number of 0's and 1's. Your goal is to select the maximum number of strings while staying within your budget of m zeros and n ones.

Example: With strings ["10", "0001", "111001", "1", "0"], m=5 zeros, and n=3 ones, you could select ["10", "0001", "1", "0"] (using 3 zeros and 3 ones) for a subset of size 4.

Input & Output

example_1.py β€” Basic Case
$ Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3
β€Ί Output: 4
πŸ’‘ Note: The largest subset is ["10", "0001", "1", "0"] which contains at most 5 zeros and 3 ones. String "111001" would exceed the ones limit.
example_2.py β€” Tight Constraints
$ Input: strs = ["10","0","1"], m = 1, n = 1
β€Ί Output: 2
πŸ’‘ Note: We can choose ["0", "1"] using exactly 1 zero and 1 one, achieving the maximum possible size of 2.
example_3.py β€” All Same Strings
$ Input: strs = ["111","111","111"], m = 0, n = 2
β€Ί Output: 0
πŸ’‘ Note: Each string needs 3 ones, but we only have budget for 2 ones. No strings can be selected.

Visualization

Tap to expand
Binary String Knapsack VisualizationAvailable Strings (Treasures):"10"πŸ’°1 πŸ₯‡1"111001"πŸ’°2 πŸ₯‡4"0001"πŸ’°3 πŸ₯‡1"1"πŸ’°0 πŸ₯‡1"0"πŸ’°1 πŸ₯‡0Knapsack Capacity:πŸ’° Max: 5 zerosπŸ₯‡ Max: 3 onesDP Decision Process:Step 1: Consider "10" (πŸ’°1, πŸ₯‡1)β€’ Option A: Don't take β†’ dp[1][j][k] = dp[0][j][k]β€’ Option B: Take (if jβ‰₯1, kβ‰₯1) β†’ dp[1][j][k] = dp[0][j-1][k-1] + 1β€’ Result: dp[1][j][k] = max(Option A, Option B)Step 2: Consider "111001" (πŸ’°2, πŸ₯‡4)β€’ This exceeds our gold limit (4 > 3), so we skip itβ€’ Only Option A is viable for most statesFinal Selection:Selected Subset: ["10", "0001", "1", "0"]Total Cost: πŸ’°5 zeros (≀5 βœ“), πŸ₯‡3 ones (≀3 βœ“)Maximum Subset Size: 4Algorithm: 3D DP β†’ O(lenΓ—mΓ—n) time, O(mΓ—n) space (optimized)
Understanding the Visualization
1
Count Resources
Each string "costs" a certain number of 0s and 1s
2
DP Decision
For each string: include it (if affordable) or skip it
3
Optimal Choice
Take the maximum between including vs excluding each string
4
Build Solution
Final answer is dp[length][m][n] - max strings with given constraints
Key Takeaway
🎯 Key Insight: Transform the problem into a 2D knapsack where each string has dual costs. Use DP to build optimal solutions incrementally, ensuring we never exceed our zero/one budgets.

Time & Space Complexity

Time Complexity
⏱️
O(len Γ— m Γ— n)

We fill a 3D table of size len Γ— m Γ— n, each cell taking O(1) time

n
2n
βœ“ Linear Growth
Space Complexity
O(len Γ— m Γ— n)

3D DP table to store all possible states

n
2n
⚑ Linearithmic Space

Constraints

  • 1 ≀ strs.length ≀ 600
  • 1 ≀ strs[i].length ≀ 100
  • strs[i] consists only of digits '0' and '1'
  • 1 ≀ m, n ≀ 100
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
67.2K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen