2
public List<String> findAllRecipes(3
String[] recipes, List<List<String>> ingredients, String[] supplies) {4
HashSet<String> sup = new HashSet<>();5
HashMap<String, Integer> index = new HashMap<>();6
HashMap<String, List<String>> map = new HashMap<>();8
// create hashset of supplies9
for (String s : supplies) {13
// store index of all recipes14
for (int i = 0; i < recipes.length; i++) {15
index.put(recipes[i], i);18
int[] indegree = new int[recipes.length];19
// create a mapping of all the recipes that are Ingredients as well20
// to the recipes they are ingredients for21
for (int i = 0; i < recipes.length; i++) {22
for (String need : ingredients.get(i)) {23
if (sup.contains(need)) continue;25
map.putIfAbsent(need, new ArrayList<String>());26
map.get(need).add(recipes[i]);31
LinkedList<Integer> q = new LinkedList<>();32
// add all the recipes with indegree 0 to the queue33
for (int i = 0; i < recipes.length; i++) {34
if (indegree[i] == 0) {39
List<String> cooked = new ArrayList<>();40
while (!q.isEmpty()) {42
cooked.add(recipes[i]);44
if (!map.containsKey(recipes[i])) {45
// if the map does not contain this recipe, this means46
// this recipe is not an ingredient for any other recipe47
// and no further processing is required51
for (String recipe : map.get(recipes[i])) {52
if (--indegree[index.get(recipe)] == 0) {53
q.add(index.get(recipe));