LeetCode Challenge Day 57 β 474. Ones and Zeroes
Nitin Ahirwal / November 11, 2025
Hey folks π
This is Day 57 of my LeetCode streak π
Todayβs problem is 474. Ones and Zeroes β a medium-level dynamic programming problem that generalizes the classic 0/1 knapsack into two dimensions β zeros and ones.
The goal is to pick the largest subset of strings such that the total number of zeros β€ m and ones β€ n.
π Problem Statement
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 and at most n ones in the subset.
Example 1:
Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3
Output: 4
Explanation: The largest subset with at most 5 zeros and 3 ones is 0.
Example 2:
Input: strs = ["10","0","1"], m = 1, n = 1
Output: 2
Explanation: The largest subset is 1.
π‘ Intuition
This problem resembles a knapsack problem β each binary string is like an βitemβ with a cost in two dimensions:
- Cost in zeros
- Cost in ones
You have limited capacities (m zeros and n ones).
Your task is to choose as many items (strings) as possible without exceeding these capacities.
π Approach
-
Define a DP table:
Letdp[i][j]represent the maximum number of strings you can form with at mostizeros andjones. -
Process each string:
- Count the number of zeros and ones in the string.
- Traverse the DP table in reverse order (from
mtozeros,ntoones) to ensure each string is only used once.
-
Transition:
-
Result:
The answer is stored indp[m][n].
β±οΈ Complexity Analysis
-
Time Complexity:
( O(L \times m \times n) ), whereL= number of strings.
Each string updates the entire DP table once. -
Space Complexity:
( O(m \times n) ) β using a 2D array for DP.
π§βπ» Code (JavaScript)
/**
* @param {string[]} strs
* @param {number} m
* @param {number} n
* @return {number}
*/
var findMaxForm = function(strs, m, n) {
// dp[i][j] = max number of strings we can form with at most i zeros and j ones
const dp = Array.from({length: m + 1}, () => Array(n + 1).fill(0));
for (const s of strs) {
// count zeros and ones in current string
let zeros = 0, ones = 0;
for (let ch of s) {
if (ch === '0') zeros++;
else ones++;
}
// update dp in reverse to avoid reuse of the same string
for (let i = m; i >= zeros; i--) {
for (let j = n; j >= ones; j--) {
dp[i][j] = Math.max(dp[i][j], dp[i - zeros][j - ones] + 1);
}
}
}
return dp[m][n];
};
π§© Example Walkthrough
Input:
strs = ["10", "0001", "111001", "1", "0"], m = 5, n = 3
Process:
-
"10" β (1 zero, 1 one)
-
"0001" β (3 zeros, 1 one)
-
"111001" β (2 zeros, 4 ones)
-
"1" β (0 zeros, 1 one)
-
"0" β (1 zero, 0 ones)
You can pick 0 β total = 4 strings.
Output:
4
π― Reflection
This problem beautifully illustrates how multi-dimensional constraints extend classical DP problems. By adapting knapsack logic to track both zeros and ones, we efficiently determine the largest subset size possible.
Thatβs it for Day 57 of my LeetCode challenge πͺ Keep learning, keep optimizing β one DP state at a time β‘
Happy Coding π¨βπ»