Graphs Union-find
469. Satisfiability of Equality Equations
You are given equations, a list of equations between variables. Every variable is a single lowercase letter.
Each equation is a string of four characters, written either as "x==y", which says that x and y are
equal, or as "x!=y", which says that they are different. Here x and y stand for any two letters, and they
can be the same letter.
Return true if you can give each variable an integer value so that all the equations hold at the same time.
Otherwise return false. A variable has one value in every equation it appears in. Two different variables may
have the same value unless an equation says they differ.
Example 1
Input: equations = ["x==y", "z!=x", "y==z"]
falseExplanation: x == y and y == z make x equal to z. But z != x needs them to be different, so no values work.
Example 2
Input: equations = ["u!=v", "v!=w", "w!=u", "w==t"]
trueExplanation: Give u the value 1, v the value 2, and both w and t the value 3. Every equation holds.
Example 3
Input: equations = ["k!=k"]
falseExplanation: A variable always equals itself, so k != k can never hold.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ equations.length ≤ 105equations[i].length == 4equations[i][0]andequations[i][3]are lowercase English letters.equations[i][1]is'='or'!', andequations[i][2]is'='.
Clarifying questions
What you would ask an interviewer before writing any code.