class Solution {
public List findAllRecipes(String[] recipes, List> ingredients, String[] supplies) {
List ans = new ArrayList<>();
Set available = new HashSet<>(Arrays.asList(supplies));
Map> ingredientToRecipes = new HashMap<>();
Map inDegree = new HashMap<>();
for (int i = 0; i < recipes.length; ++i) {
int nonAvailable = 0;
for (String ing : ingredients.get(i)) {
if (!available.contains(ing)) {
ingredientToRecipes.computeIfAbsent(ing, s -> new HashSet<>()).add(recipes[i]);
++nonAvailable;
}
}
if (nonAvailable == 0) {
ans.add(recipes[i]);
}else {
inDegree.put(recipes[i], nonAvailable);
}
}
for (int i = 0; i < ans.size(); ++i) {
String recipe = ans.get(i);
if (ingredientToRecipes.containsKey(recipe)) {
for (String rcp : ingredientToRecipes.remove(recipe)) {
if (inDegree.merge(rcp, -1, Integer::sum) == 0) {
ans.add(rcp);
}
}
}
}
return ans;
}
}