Evaluate Division
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.

30:00

Evaluate Division
medium
Topics
Companies

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.

Example 1:
Input: {"equations":[["a","b"],["b","c"]],"values":[2,3],"queries":[["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]}
Output: [6.0,0.5,-1.0,1.0,-1.0]
Constraints:
  • 1equations.length201 \leq \text{equations.length} \leq 20

  • equations[i].length==2\text{equations}[i].\text{length} == 2

  • 1Ai.length,Bi.length51 \leq A_i.\text{length}, B_i.\text{length} \leq 5

  • values.length==equations.length\text{values.length} == \text{equations.length}

  • 0.0<values[i]20.00.0 < \text{values}[i] \leq 20.0

  • 1queries.length201 \leq \text{queries.length} \leq 20

  • queries[i].length==2\text{queries}[i].\text{length} == 2

  • A_i, B_i, C_j, D_j consist of lowercase letters and digits.

Input
arr ={"equations":[["a","b"],["b","c"]],"values":[2,3],"queries":[["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]}

Built graph with 3 variables

2.00.53.00.3

a

b

c

Query nodes

Path

Visited

Variables
No variables to display
DepthFunction Call
Stack empty
0/6