You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. Each Ai or Bi is a string that represents a single variable.

You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query where you must find the answer for Cj / Dj = ?

Return the answers to all queries. If a single answer cannot be determined, return -1.0.

Note: The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.

Note: The variables that do not occur in the list of equations are undefined, so the answer cannot be determined for them.

Input & Output

Example 1 — Basic Division Chain
$ Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
Output: [6.0,0.5,-1.0,1.0,-1.0]
💡 Note: a/b = 2.0, b/c = 3.0, so a/c = (a/b) × (b/c) = 2.0 × 3.0 = 6.0. b/a = 1/(a/b) = 0.5. a/e is undefined (-1.0). a/a = 1.0. x/x is undefined since x doesn't appear in equations.
Example 2 — Single Equation
$ Input: equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
Output: [0.5,2.0,-1.0,-1.0]
💡 Note: Only one equation a/b = 0.5. Direct query a/b = 0.5. Reverse b/a = 1/0.5 = 2.0. a/c and x/y are undefined.
Example 3 — Multiple Connected Components
$ Input: equations = [["a","b"],["c","d"]], values = [1.0,1.0], queries = [["a","c"],["b","d"],["b","a"],["d","c"]]
Output: [-1.0,-1.0,1.0,1.0]
💡 Note: Two separate components: {a,b} and {c,d}. No path between different components, so a/c and b/d are -1.0. Within components: b/a = 1/1.0 = 1.0, d/c = 1/1.0 = 1.0.

Constraints

  • 1 ≤ equations.length ≤ 20
  • equations[i].length == 2
  • 1 ≤ Ai.length, Bi.length ≤ 5
  • values.length == equations.length
  • 0.0 < values[i] ≤ 20.0
  • 1 ≤ queries.length ≤ 20
  • queries[i].length == 2
  • 1 ≤ Cj.length, Dj.length ≤ 5
  • Ai, Bi, Cj, Dj consist of lowercase English letters and digits.

Visualization

Tap to expand
Evaluate Division - DFS Graph Traversal INPUT Equations: a / b = 2.0 b / c = 3.0 Graph Structure: a b c 2.0 0.5 3.0 0.33 Queries: 1. a / c = ? 2. b / a = ? 3. a / e = ? 4. a / a = ? 5. x / x = ? ALGORITHM STEPS 1 Build Graph Create adjacency list with bidirectional weighted edges 2 For Each Query Check if both variables exist Return -1 if not in graph 3 DFS Traversal Find path from source to target Multiply edge weights along path 4 Return Result Product of weights = answer No path found = -1.0 Example: a / c = ? a b c x2 x3 Path: a --> b --> c 2.0 * 3.0 = 6.0 FINAL RESULT Query Results: a/c = 6.0 OK b/a = 0.5 OK a/e = -1.0 N/A a/a = 1.0 OK x/x = -1.0 N/A Output Array: [6.0, 0.5, -1.0, 1.0, -1.0] Complexity: Time: O(Q * (V + E)) Key Insight: Model equations as a weighted directed graph where a/b=k creates edges a-->b (weight k) and b-->a (weight 1/k). Finding x/y becomes a path-finding problem: DFS from x to y, multiplying edge weights along the path. If no path exists or variables are undefined, return -1.0. If x==y and x exists in graph, return 1.0. TutorialsPoint - Evaluate Division | DFS Graph Traversal Approach
Asked in
Google 25 Facebook 18 Amazon 15 Microsoft 12
185.0K Views
Medium Frequency
~25 min Avg. Time
4.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen