@haruhiku Here's a somewhat terse version of your code:

public double[] calcEquation(String[][] equations, double[] values, String[][] queries) { Map<String, Map<String, Double>> map = new HashMap<>(); int i = 0; for(String[] eq : equations) { map.computeIfAbsent(eq[0], v -> new HashMap<String, Double>()).put(eq[1], values[i]); map.computeIfAbsent(eq[1], v -> new HashMap<String, Double>()).put(eq[0], 1 / values[i++]); } double[] res = new double[queries.length]; i = 0; for(String[] query : queries) { String num = query[0], denom = query[1]; if (map.containsKey(num)) { double tmp = dfs(num, denom, new HashSet<>(), map, 1.0); if (tmp == 0.0) tmp = -1.0; res[i++] = tmp; } else { res[i++] = -1.0; } } return res; } private double dfs(String curr, String end, Set<String> visited, Map<String, Map<String, Double>> map, double distance) { if (curr.equals(end)) return distance; if (map.get(curr).containsKey(end)) return map.get(curr).get(end) * distance; double tmp = 0.0; for(String neighbor : map.get(curr).keySet()) { if (visited.add(neighbor)) { tmp = dfs(neighbor, end, visited, map, distance * map.get(curr).get(neighbor)); if (tmp != 0.0) break; } } return tmp; }Evaluate Division