Ones and Zeroes

medium array string dp

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 0s and n 1s in the subset.

Inputstrs = ["10","0001","111001","1","0"], m = 5, n = 3
Output4
Take {"10","0001","1","0"} → 4 strings using 4 zeros and 3 ones.

def find_max_form(strs, m, n):
    dp = [[0] * (n + 1) for _ in range(m + 1)]
    for s in strs:
        z = s.count('0'); o = s.count('1')
        for i in range(m, z - 1, -1):
            for j in range(n, o - 1, -1):
                dp[i][j] = max(dp[i][j], dp[i - z][j - o] + 1)
    return dp[m][n]
function findMaxForm(strs, m, n) {
  const dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
  for (const s of strs) {
    let z = 0, o = 0;
    for (const c of s) c === '0' ? z++ : o++;
    for (let i = m; i >= z; i--) {
      for (let j = n; j >= o; j--) {
        dp[i][j] = Math.max(dp[i][j], dp[i - z][j - o] + 1);
      }
    }
  }
  return dp[m][n];
}
class Solution {
    public int findMaxForm(String[] strs, int m, int n) {
        int[][] dp = new int[m + 1][n + 1];
        for (String s : strs) {
            int z = 0, o = 0;
            for (char c : s.toCharArray()) { if (c == '0') z++; else o++; }
            for (int i = m; i >= z; i--) {
                for (int j = n; j >= o; j--) {
                    dp[i][j] = Math.max(dp[i][j], dp[i - z][j - o] + 1);
                }
            }
        }
        return dp[m][n];
    }
}
int findMaxForm(vector<string>& strs, int m, int n) {
    vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
    for (auto& s : strs) {
        int z = count(s.begin(), s.end(), '0');
        int o = (int)s.size() - z;
        for (int i = m; i >= z; i--)
            for (int j = n; j >= o; j--)
                dp[i][j] = max(dp[i][j], dp[i - z][j - o] + 1);
    }
    return dp[m][n];
}
Time: O(L · m · n) where L is total chars Space: O(m · n)