Find All Possible Recipes from Given Supplies

Medium#2115

You get a list of recipes, the ingredients of each recipe and the supplies you have plenty of. An ingredient is either a supply or another recipe, which has to be made first; anything else cannot be had. Return every recipe that can be made, in any order. Our example: dough from flour, water; pasta from dough, eggs; crepe from flour, eggs, milk; cake from crepe, sugar. The supplies: flour, water, eggs, sugar. The answer is dough and pasta.

input
recipes: the names of the recipes; ingredients[i]: what recipe recipes[i] is made from. supplies: what you have plenty of. An ingredient is either a supply or another recipe, which has to be made first; anything else cannot be had.
returns
Every recipe that can be made, in any order.
constraints
  • n = recipes.length = ingredients.length, 1 ≤ n ≤ 100
  • 1 ≤ ingredients[i].length ≤ 100
  • 1 ≤ supplies.length ≤ 100
  • every name is 1 to 10 lowercase letters
  • the recipes and the supplies are all different
  • no ingredient is given twice for one recipe
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {string[]} recipes
 * @param {string[][]} ingredients
 * @param {string[]} supplies
 * @return {string[]}
 */
function findAllRecipes(recipes, ingredients, supplies) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more topological sort problems

Pattern “Topological sort” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.