Ones and Zeroes
Problem Statement
You are given a list of binary strings strs. Each string is made only of the digits 0 and 1. You are also given two budgets: m is the most zeros you may use in total, and n is the most ones you may use in total.
Pick as many strings as you can, so that the strings you pick use at most m zeros and at most n ones together. Each string can be picked at most once.
Return the largest number of strings you can pick. The function is int findMaxForm(vector<string>& strs, int m, int n).
strs, a list of strings made only of 0 and 1. m, the zero budget. n, the one budget.
A single number: the largest count of strings you can pick while staying within both budgets.
Pick "10", "0001", "1" and "0". They use 1 + 3 + 0 + 1 = 5 zeros and 1 + 1 + 1 + 0 = 3 ones. "111001" alone has four 1s, which is over the budget.
"0" and "1" together use one zero and one one. "10" also uses one of each, so picking it blocks both of them.
"00" uses both zeros, so nothing else fits after it. "011" and "101" together use 2 zeros and 4 ones, which fits, so the answer is 2.
- • 1 ≤ strs.length ≤ 600
- • 1 ≤ strs[i].length ≤ 100
- • strs[i] consists only of the digits '0' and '1'
- • 1 ≤ m, n ≤ 100
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Recurse on every string: skip it, or take it when both budgets can pay for it
Store dp[i][z][o] for every prefix of strings and every budget pair
Keep one dp[z][o] table; for each string, both loops go from the budget down to the string's counts
Go through the strings in order. Each one is either skipped, or taken when both budgets can pay for it. The answer is the bigger count of the two choices. Every subset of strings gets tried, so the same budget state is reached again and again.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — Knapsack translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.