2115. Find All Possible Recipes from Given Supplies

Find All Possible Recipes from Given Supplies (LeetCode 2115) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Graph BFS - Topological Sort (Kahn's Algorithm) subpattern.

How to solve Find All Possible Recipes from Given Supplies

Find All Possible Recipes from Given Supplies is a Topological Sort problem, part of Graph Traversal Patterns (DFS & BFS). Master topological sorting using Kahn's BFS algorithm. Solve course schedule and build order problems.

Work towards the solution in these steps:

  1. Can we use a data structure to quickly query whether we have a certain ingredient?
  2. Once we verify that we can make a recipe, we can add it to our ingredient data structure. We can then check if we can make more recipes as a result of this.

The reference solution runs in O(N + M) time and O(N + M + S) space.

The Find All Possible Recipes from Given Supplies solution and editorial walks through the full approach with reference code.

Problem statement

You have information about n different recipes. You are given a string array recipes and a 2D string array ingredients. The ith recipe has the name recipes[i], and you can create it if you have all the needed ingredients from ingredients[i]. A recipe can also be an ingredient for other recipes, i.e., ingredients[i] may contain a string that is in recipes.

You are also given a string array supplies containing all the ingredients that you initially have, and you have an infinite supply of all of them.

Return a list of all the recipes that you can create. You may return the answer in any order.

Note that two recipes may contain each other in their ingredients.

Example 1:

Input: recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"] Output: ["bread"] Explanation: We can create "bread" since we have the ingredients "yeast" and "flour".

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 the ingredients "yeast" and "flour". We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".

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" since we have the ingredients "yeast" and "flour". We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread". We can create "burger" since we have the ingredient "meat" and can create the ingredients "bread" and "sandwich".

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 the values of recipes and supplies combined are unique.

- Each ingredients[i] does not contain any duplicate values.

Topics and companies

Topics: Array, Hash Table, String, Graph, Topological Sort.

How to practise Find All Possible Recipes from Given Supplies on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N + M) time and O(N + M + S) space.

Open Find All Possible Recipes from Given Supplies in the code editor, or read the Find All Possible Recipes from Given Supplies editorial for a worked solution with its approach and complexity analysis.

Where Find All Possible Recipes from Given Supplies sits in the DSA pattern sheet

Problems related to Find All Possible Recipes from Given Supplies

Other problems that use the same Graph BFS - Topological Sort (Kahn's Algorithm) and Graph Traversal Patterns (DFS & BFS) techniques:

Find All Possible Recipes from Given Supplies FAQ

What is the LeetCode number of Find All Possible Recipes from Given Supplies?

Find All Possible Recipes from Given Supplies is LeetCode problem 2115, rated Medium.

What pattern is used to solve Find All Possible Recipes from Given Supplies?

Find All Possible Recipes from Given Supplies is a Topological Sort problem. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Graph BFS - Topological Sort (Kahn's Algorithm) subpattern.

What is the time complexity of Find All Possible Recipes from Given Supplies?

The reference solution runs in O(N + M) time and O(N + M + S) space.

Preparing this workspace.