---
# System prepended metadata

title: LC 2115. Find All Possible Recipes from Given Supplies
tags: [medium, leedcode, Topological Sort, c++]

---

# LC 2115. Find All Possible Recipes from Given Supplies

### [Problem link](https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies/)

###### tags: `leedcode` `medium` `c++` `Topological Sort`

You have information about <code>n</code> different recipes. You are given a string array <code>recipes</code> and a 2D string array <code>ingredients</code>. The <code>i<sup>th</sup></code> recipe has the name <code>recipes[i]</code>, and you can  **create**  it if you have  **all**  the needed ingredients from <code>ingredients[i]</code>. Ingredients to a recipe may need to be created from  **other recipes**, i.e., <code>ingredients[i]</code> may contain a string that is in <code>recipes</code>.

You are also given a string array <code>supplies</code> 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:** 

- <code>n == recipes.length == ingredients.length</code>
- <code>1 <= n <= 100</code>
- <code>1 <= ingredients[i].length, supplies.length <= 100</code>
- <code>1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10</code>
- <code>recipes[i], ingredients[i][j]</code>, and <code>supplies[k]</code> consist only of lowercase English letters.
- All the values of <code>recipes</code> and <code>supplies</code>combined are unique.
- Each <code>ingredients[i]</code> does not contain any duplicate values.

## Solution 1
#### C++
```cpp=
class Solution {
public:
    vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
        unordered_map<string, vector<string>> depend;
        unordered_map<string, int> indegree;
        for (int i = 0; i < recipes.size(); i++) {
            for (string& ing: ingredients[i]) {
                depend[ing].push_back(recipes[i]);
                indegree[recipes[i]]++;
            }
        }

        queue<string> q;
        for (string& supply: supplies) {
            q.push(supply);
        }

        vector<string> ans;
        while (!q.empty()) {
            string node = q.front();
            q.pop();

            for (string& nextNode: depend[node]) {
                indegree[nextNode]--;
                if (indegree[nextNode] == 0) {
                    q.push(nextNode);
                    ans.push_back(nextNode);
                }
            }
        }
        return ans;
    }
};
```

>### Complexity
>n = recipes.size()
>m = max length of ingredients[i]
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(nm)           | O(nm)            |

## Note
x
