Satisfiability of Equality Equations

Given an arrayequations of strings that represent relationships between variables, each stringequations[i] has length4and takes one of two different forms:"a==b"or"a!=b". Here,aandbare lowercase letters (not necessarily different) that represent one-letter variable names.

Returntrue if and only if it is possible to assign integers to variable names so as to satisfy all the given equations.

Example

Example 1:

Input: 
["a==b","b!=a"]
Output: 
false
Explanation: 
If we assign say, a = 1 and b = 1, 
then the first equation is satisfied, but not the second.  
There is no way to assign the variables to satisfy both equations.

Example 2:

Input: 
["b==a","a==b"]
Output: 
true
Explanation: 
We could assign a = 1 and b = 1 to satisfy both equations.

Example 3:

Example 4:

Example 5:

Note

静态联通性,使用Union-Find

等号全部连接在一起,再单独判断不等号,如果连接:就false了

Code

Last updated