FastPrepLargest Binary-String Subset Within Bit Budgets

Largest Binary-String Subset Within Bit Budgets

Amazon logoAmazonâ—Ź MediumFULLTIMENEW GRADPHONE SCREEN

Problem statement

Given an array of binary strings strs and two budgets, maxOnes and maxZeroes, return the maximum number of strings you can select.

The selected strings must contain at most maxOnes ones in total and at most maxZeroes zeroes in total. Each array position may be selected at most once, including when two positions contain equal strings.

Function

largestBoundedSubset(strs: String[], maxOnes: int, maxZeroes: int) → int

Examples

Example 1

strs = ["100","10","1","11","111"]maxOnes = 3maxZeroes = 0return = 2

Select "1" and "11". They use exactly three ones and no zeroes.

Example 2

strs = ["10","0001","111001","1","0"]maxOnes = 3maxZeroes = 5return = 4

The strings "10", "0001", "1", and "0" use three ones and five zeroes.

Example 3

strs = ["10","0","1"]maxOnes = 1maxZeroes = 1return = 2

Selecting "0" and "1" uses both budgets and yields two strings.

Constraints

  • 1 <= strs.length <= 600.
  • 1 <= strs[i].length <= 100.
  • Every strs[i] contains only 0 and 1.
  • 0 <= maxOnes, maxZeroes <= 100.

More Amazon problems

See Amazon hiring insights
Saved
public int largestBoundedSubset(String[] strs, int maxOnes, int maxZeroes) {
  // write your code here
}
Checking account…