Given an array equations of strings that represent relationships between variables, each string equations[i] has length 4 and takes one of two different forms: “a==b” or “a!=b”. Here, a and b are lowercase letters (not necessarily different) that represent one-letter variable names.
Return true if and only if it is possible to assign integers to variable names so as to satisfy all the given equations.
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:
Input: ["a==b","b==c","a==c"] Output: true Example 4:
Input: ["a==b","b!=c","c==a"] Output: false Example 5:
Input: ["c==c","b==d","x!=z"] Output: true
Problem Analysis
We have 26 nodes in the graph. All “==” equations represent the connection in the graph. The connected nodes should be in the same union or set.
Then we check all inequations. Two unequal nodes should be in a different union or set.
Algorithm Analysis
Use two Sets
Traversing the equation of == and put both first and second character into the set. Then, traverse the equation of != and check if first is inside the set of second and second is inside the set of first.
Union Find
First, pass all == equations. Union equal letters together Now we know which letters must equal to the others. Second pass all != inequations, Check if there are any contradict happens.