Satisfiability Of Equality Equations Problem
Description
LeetCode Problem 990.
You are given an array of strings equations that represent relationships between variables where each string equations[i] is of length 4 and takes one of two different forms: "x_i==y_i" or "x_i!=y_i".Here, x_i and y_i are lowercase letters (not necessarily different) that represent one-letter variable names.
Return true if it is possible to assign integers to variable names so as to satisfy all the given equations, or false otherwise.
Example 1:
Input: equations = ["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: equations = ["b==a","a==b"]
Output: true
Explanation: We could assign a = 1 and b = 1 to satisfy both equations.
Example 3:
Input: equations = ["a==b","b==c","a==c"]
Output: true
Example 4:
Input: equations = ["a==b","b!=c","c==a"]
Output: false
Example 5:
Input: equations = ["c==c","b==d","x!=z"]
Output: true
Constraints:
- 1 <= equations.length <= 500
- equations[i].length == 4
- equations[i][0] is a lowercase letter.
- equations[i][1] is either '=' or '!'.
- equations[i][2] is '='.
- equations[i][3] is a lowercase letter.
Solution
Approach: Union-Find over the 26 Letters
Equality is transitive: a==b and b==c force a==c even though that equation never appears. So the == equations split the letters into groups that must all get the same value. Apart from that we are free, so give every group its own distinct number. That assignment satisfies every == equation, and it breaks a != equation only when both letters are in the same group. So the answer is false exactly when some x!=y has x and y in one group.
Union-find (disjoint set union) builds these groups:
- Start with each letter as its own group:
parent[i] = i. - First pass: for every
x==y, merge the groups ofxandy:parent[find(x)] = find(y). - Second pass: for every
x!=y, iffind(x) == find(y), returnfalse. - Otherwise return
true.
The two passes are the key detail. Checking each != as it appears is wrong, because an == later in the list can still join the two letters: in ["a!=b", "a==b"] the conflict only shows up after the second equation. Note also that "a!=a" is a valid input and must return false; the second pass handles it, since find(a) == find(a).
find follows parent links to the root of the group, and path halving (pointing a node to its grandparent while walking) keeps the trees short.
Complexity: O(n) time, where n is the number of equations, since each union-find operation on 26 letters is effectively constant. O(1) extra space.
class Solution:
def equationsPossible(self, equations: List[str]) -> bool:
parent = list(range(26)) # union-find over the 26 letters
def find(x: int) -> int:
while parent[x] != x:
parent[x] = parent[parent[x]] # path halving keeps the trees flat
x = parent[x]
return x
# First merge every pair that must be equal.
for eq in equations:
if eq[1] == "=":
a, b = ord(eq[0]) - ord("a"), ord(eq[3]) - ord("a")
parent[find(a)] = find(b)
# Then no "!=" pair may end up in the same group.
for eq in equations:
if eq[1] == "!":
a, b = ord(eq[0]) - ord("a"), ord(eq[3]) - ord("a")
if find(a) == find(b):
return False
return True
Now write it from memory
Close the write-up and solve it yourself against the tests.