Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #474: Ones and Zeroes

In this guide, we solve Leetcode #474 Ones and Zeroes in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

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

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Array, String, Dynamic Programming

Intuition

The problem breaks into overlapping subproblems, so caching results prevents exponential repetition.

A carefully chosen DP state captures exactly what we need to build the final answer.

Approach

Define the DP state and recurrence, then compute states in the correct order.

Optionally compress space once the recurrence is clear.

Steps:

  • Choose a DP state definition.
  • Write the recurrence and base cases.
  • Compute states in the correct order.

Example

Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3 Output: 4 Explanation: The largest subset with at most 5 0's and 3 1's is {"10", "0001", "1", "0"}, so the answer is 4. Other valid but smaller subsets include {"0001", "1"} and {"10", "1", "0"}. {"111001"} is an invalid subset because it contains 4 1's, greater than the maximum of 3.

Python Solution

class Solution: def findMaxForm(self, strs: List[str], m: int, n: int) -> int: sz = len(strs) f = [[[0] * (n + 1) for _ in range(m + 1)] for _ in range(sz + 1)] for i, s in enumerate(strs, 1): a, b = s.count("0"), s.count("1") for j in range(m + 1): for k in range(n + 1): f[i][j][k] = f[i - 1][j][k] if j >= a and k >= b: f[i][j][k] = max(f[i][j][k], f[i - 1][j - a][k - b] + 1) return f[sz][m][n]

Complexity

The time complexity is O(sz×m×n)O(sz \times m \times n)O(sz×m×n), and the space complexity is O(sz×m×n)O(sz \times m \times n)O(sz×m×n), where szszsz is the length of the array strsstrsstrs, and mmm and nnn are the upper limits on the number of zeros and ones, respectively. The space complexity is O(sz×m×n)O(sz \times m \times n)O(sz×m×n), where szszsz is the length of the array strsstrsstrs, and mmm and nnn are the upper limits on the number of zeros and ones, respectively.

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy