Find the largest subset of binary strings within zero and one limits

Quick Overview

Given a list of binary strings and limits on the total number of zeros and ones, find how many strings the largest valid subset can hold. The problem tests whether you recognize a two-dimensional 0/1 knapsack and fill the table so that each string is used only once.

Find the largest subset of binary strings within zero and one limits

Company: Nokia

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

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, counted over all of its strings together. A set `x` is a subset of a set `y` if all elements of `x` are also elements of `y`. ### Function Signature ```python def find_max_form(strs: list[str], m: int, n: int) -> int: ``` ### Rules - Each element of `strs` can be chosen at most once. Equal strings at different positions are separate elements, so both copies may be chosen. - `m` limits the total number of `'0'` characters and `n` limits the total number of `'1'` characters across the chosen strings. - The empty subset is allowed, so the answer is 0 when no single string fits. ### Constraints - `1 <= strs.length <= 600` - `1 <= strs[i].length <= 100` - `strs[i]` consists only of digits `'0'` and `'1'`. - `1 <= m, n <= 100` ### Examples ```text Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3 Output: 4 ``` The subset {"10", "0001", "1", "0"} uses 5 zeros and 3 ones. "111001" has 4 ones by itself, so no valid subset can contain it, and the remaining four strings fit together. ```text Input: strs = ["10","0","1"], m = 1, n = 1 Output: 2 ``` {"0", "1"} uses one zero and one one. Taking all three strings would need 2 zeros, so 3 is not possible. ```text Input: strs = ["11","001","001"], m = 4, n = 2 Output: 2 ``` The two copies of "001" use 4 zeros and 2 ones. Any subset containing "11" has already spent both ones, so it cannot also hold a "001".

Overview: Given a list of binary strings and limits on the total number of zeros and ones, find how many strings the largest valid subset can hold. The problem tests whether you recognize a two-dimensional 0/1 knapsack and fill the table so that each string is used only once.

Nokia logo
Nokia
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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, counted over all of its strings together.

A set x is a subset of a set y if all elements of x are also elements of y.

Function Signature

def find_max_form(strs: list[str], m: int, n: int) -> int:

Rules

  • Each element of strs can be chosen at most once. Equal strings at different positions are separate elements, so both copies may be chosen.
  • m limits the total number of '0' characters and n limits the total number of '1' characters across the chosen strings.
  • The empty subset is allowed, so the answer is 0 when no single string fits.

Constraints

  • 1 <= strs.length <= 600
  • 1 <= strs[i].length <= 100
  • strs[i] consists only of digits '0' and '1' .
  • 1 <= m, n <= 100

Examples

Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3
Output: 4

The subset {"10", "0001", "1", "0"} uses 5 zeros and 3 ones. "111001" has 4 ones by itself, so no valid subset can contain it, and the remaining four strings fit together.

Input: strs = ["10","0","1"], m = 1, n = 1
Output: 2

{"0", "1"} uses one zero and one one. Taking all three strings would need 2 zeros, so 3 is not possible.

Input: strs = ["11","001","001"], m = 4, n = 2
Output: 2

The two copies of "001" use 4 zeros and 2 ones. Any subset containing "11" has already spent both ones, so it cannot also hold a "001".