Find All Possible Recipes from Given Supplies

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

You have information about n recipes. You are given a string array recipes and a 2D string array ingredients, where ingredients[i] is a list of items needed to create recipes[i]. You are also given a string array supplies containing all the ingredients that you initially have, and you have an infinite supply of each.

Return a list of all the recipes that you can create. A recipe can be an ingredient of another recipe, meaning you may use the output of one recipe as an ingredient for another.

You may return the answer in any order.

Examples

Example 1:

Input: recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"]

Output: ["bread"]

Explanation: We have all the ingredients needed for "bread", so we can create it.

Example 2:

Input: recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]

Output: ["bread","sandwich"]

Explanation: We can create "bread" since we have "yeast" and "flour". Then we can create "sandwich" since we now have "bread" and "meat".

Example 3:

Input: recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]

Output: ["bread","sandwich","burger"]

Explanation: We can create "bread", then "sandwich" (needs "bread" + "meat"), then "burger" (needs "sandwich" + "meat" + "bread").

Constraints

  • n == recipes.length == ingredients.length
  • 1 <= n <= 100
  • 1 <= ingredients[i].length, supplies.length <= 100
  • 1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10
  • recipes[i], ingredients[i][j], and supplies[k] consist only of lowercase English letters
  • All values of recipes and supplies combined are unique
  • Each ingredients[i] does not contain any duplicate values
Source: Topological Sort pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle