Count recipes a pantry can cover

0
Sign in

You keep a pantry of ingredients, written as a string pantry. Each character is one unit of an ingredient, so "aab" holds two units of a and one unit of b.

You also have a list of strings recipes. Each recipe lists the units it needs in the same way: every character is one unit, and a character that appears twice needs two units.

Implement:

def count_makeable(recipes: list[str], pantry: str) -> int: ...

Return how many recipes the pantry can supply. A recipe can be made when, for every character, the pantry has at least as many units as the recipe needs. Check each recipe on its own against the full pantry: making one recipe does not use up units for the others.

Constraints

  • 0 <= len(recipes) <= 10^4
  • 0 <= len(pantry), len(recipes[i]) <= 10^4
  • Characters are case-sensitive. An empty recipe needs nothing and can always be made.

Example 1

recipes = ["bread", "dab", "zz"]
pantry  = "abbreadd"
output  = 2

"bread" and "dab" are both covered. "zz" needs two z units and the pantry has none.

Example 2

recipes = ["aa", "a", ""]
pantry  = "a"
output  = 2

"aa" needs two units of a but only one is available. "a" and the empty recipe can both be made.

Reconstructed from the reported question; details may differ from what interviewers ask.

MicrosoftCodingEasy

Sign in to see the worked answer

The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.