236. Evaluate Division

MediumGraph

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 and Bi is a string of lowercase letters. You are also given queries, where queries[j] = [Cj, Dj] asks for the value of Cj / Dj. Return the answers to all queries. If a single answer cannot be determined, return -1.0. Note: the input equations are always valid; no equation results in division by zero and there is no contradiction. In this catalog the values are chosen so every answer is an exact number. Variables not appearing in any equation give -1.0.

Examples

Input: [["a","b"],["b","c"]] [2.0,4.0] [["a","c"],["b","a"],["a","e"],["a","a"],["c","a"]]

Output: [8.0,0.5,-1.0,1.0,0.125]

Explanation: a/b=2, b/c=4 give a/c=8; unknown variable e yields -1; a/a=1.

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
Loading...

Run checks all cases above. Submit evaluates all test cases.