537. Check for Contradictions in Equations

You are given a 2D array of strings equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] means that Ai / Bi = values[i].

Determine if there exists a contradiction in the equations. Return true if there is a contradiction, or false otherwise.

Note :

  • When checking if two numbers are equal, check that their absolute difference is less than 10-5.
  • The testcases are generated such that there are no cases targeting precision, i.e. using double is enough to solve the problem.

Example 1:

Input : equations = [ ["a", "b"], ["c", "a"], ["a", "a"] ], values = [1.5, 2.0, 1.0]

Output : false

Explanation :

The given equations are: a / b = 1.5, c / a = 2.0, a / a = 1.0

There are no contradictions in the equations. One possible assignment to satisfy all equations is:

a = 3, b = 2 and c = 6.

Example 2:

Input : equations = [ ["a", "b"], ["c", "a"], ["a", "a"] ], values = [1.5, 2.0, 2.0]

Output : false

Explanation :

The given equations are: a / b = 1.5, c / a = 2.0, a / a = 2.0

Since the third equation is a / a= 2.0, we get a contradiction as same value division is 1.0.

Now Your Turn!

Pick the correct output for the given input

Input : equations = [ ["take", "u"], ["u", "for"], ["ward", "for"] ], values = [2.0, 0.5, 3.0]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= equations.length <= 100
  • equations[i].length == 2
  • 1 <= Ai.length, Bi.length <= 5
  • Ai, Bi consist of lowercase English letters.
  • equations.length == values.length
  • 0.0 < values[i] <= 10.0
  • values[i] has a maximum of 2 decimal places.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
bool checkContradictions(vector<vector<string>>& equations, vector<double>& values) {
//your code goes here
}
};
Test Case

Input:

Equations
Values