15% off all plans & roadmaps Use code
See plansProblem editorial
You are given symbolic division relationships in equations. Each two-element entry [a, b] states that a / b equals the corresponding number in values at the same index. Every variable is represented by a string, and variables may be connected through several equations.
Evaluate Division is a Graph Traversal Patterns (DFS & BFS) problem, in the Graph DFS - Connected Components / Island Counting subpattern. The pattern covers dFS/BFS traversal, topological sort, shortest paths, Union-Find, and MST
The reference solution runs in O(E + Q(V + E)), where E is the number of equations, V is the number of variables, and Q is the number of queries. time and O(V + E) for the graph, visited sets, and recursion stack. space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Evaluate Division is a Graph Traversal Patterns (DFS & BFS) problem, in the Graph DFS - Connected Components / Island Counting subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.