Ones and Zeroes — Solution in Python, Java & C++
Ones and Zeroes solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem on arrays and strings.
Ones and Zeroes is a medium coding problem on arrays, strings and dynamic programming, asked in Amazon, Google and Adobe interviews.
- Difficulty: Medium
- Topics: Arrays, Strings, Dynamic Programming
- Asked at: Amazon, Google, Adobe
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
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 that uses at most m zeros and n ones in total.
Example 1
Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3
Output: 4
Explanation: One largest subset is {"10","0001","1","0"}.
Example 2
Input: strs = ["10","0","1"], m = 1, n = 1
Output: 2
Explanation: Either {"10"} alone or {"0","1"} — the latter is bigger.
Example 3
Input: strs = ["0"], m = 0, n = 0
Output: 0
Constraints
1 <= strs.length <= 201 <= strs[i].length <= 10strs[i] consists of '0' and '1'.0 <= m, n <= 20
Hints for Ones and Zeroes
Hint 1
This is a knapsack with two capacities — zeros and ones — instead of one.
Hint 2
dp[i][j]is the largest subset using at mostizeros andjones.Hint 3
Process one string at a time and iterate both capacities downward, so each string is used at most once.
How to solve Ones and Zeroes
A 0/1 knapsack with two capacities: dp[i][j] is the largest subset using at most i zeros and j ones; iterate both capacities downward per string.
Approach
- For each string count its zeros and ones.
- For
ifrommdown tozerosandjfromndown toones,dp[i][j] = max(dp[i][j], dp[i-zeros][j-ones] + 1). - Return
dp[m][n].
Why it works
Each string is either taken or not; the downward sweep guarantees the value read from dp[i-zeros][j-ones] predates the current string, so it is used at most once.
Complexity
- Time —
O(L · m · n) forLstrings - Space —
O(m · n)
Pitfalls
- Iterating upward turns it into an unbounded knapsack and overcounts.
Reference solution
Python
def findMaxForm(strs, m: int, n: int) -> int:
dp = [[0] * (n + 1) for _ in range(m + 1)]
for s in strs:
zeros = s.count("0")
ones = len(s) - zeros
for i in range(m, zeros - 1, -1):
for j in range(n, ones - 1, -1):
dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1)
return dp[m][n]Java
public static int findMaxForm(String[] strs, int m, int n) {
int[][] dp = new int[m + 1][n + 1];
for (String s : strs) {
int zeros = 0, ones = 0;
for (int k = 0; k < s.length(); k++) {
if (s.charAt(k) == '0') zeros++;
else ones++;
}
for (int i = m; i >= zeros; i--) {
for (int j = n; j >= ones; j--) {
dp[i][j] = Math.max(dp[i][j], dp[i - zeros][j - ones] + 1);
}
}
}
return dp[m][n];
}C++
int findMaxForm(vector<string>& strs, int m, int n) {
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (string& s : strs) {
int zeros = 0, ones = 0;
for (char c : s) {
if (c == '0') zeros++;
else ones++;
}
for (int i = m; i >= zeros; i--) {
for (int j = n; j >= ones; j--) {
dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1);
}
}
}
return dp[m][n];
}JavaScript
var findMaxForm = function(strs, m, n) {
const dp = [];
for (let i = 0; i <= m; i++) dp.push(new Array(n + 1).fill(0));
for (let s = 0; s < strs.length; s++) {
let zeros = 0, ones = 0;
for (let k = 0; k < strs[s].length; k++) {
if (strs[s][k] === "0") zeros++;
else ones++;
}
for (let i = m; i >= zeros; i--) {
for (let j = n; j >= ones; j--) {
const candidate = dp[i - zeros][j - ones] + 1;
if (candidate > dp[i][j]) dp[i][j] = candidate;
}
}
}
return dp[m][n];
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.