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: "xi==yi"
or "xi!=yi"
.Here, xi
and yi
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.
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.This problem can be efficiently solved using the Union-Find algorithm. The core idea is to represent variables as nodes in a graph, and equality equations as edges connecting those nodes. We then process the inequality equations to check for inconsistencies.
Algorithm:
Initialization: Create a parent array p
of size 26 (representing lowercase letters 'a' to 'z'). Initialize each element p[i]
to i
, indicating that each variable initially belongs to its own disjoint set.
Process Equality Equations: Iterate through the input equations
. For each equation of the form "x==y":
find(x)
) and the root parent of 'y' (find(y)
).p[find(x)] = find(y)
. This ensures that all variables equivalent to 'x' will also be equivalent to 'y'.Process Inequality Equations: Iterate through the equations
again. For each equation of the form "x!=y":
find(x)
) and the root parent of 'y' (find(y)
).find(x) == find(y)
), it means 'x' and 'y' are in the same set, violating the inequality constraint. Return false
.Return True: If all inequality equations are satisfied, it means a consistent assignment is possible, so return true
.
find(x)
function: This is a crucial part of Union-Find. It recursively finds the root parent of a node x
. Path compression optimization is used to improve efficiency. When a root is found, all nodes along the path to the root are updated to point directly to the root.
Time Complexity Analysis:
find
operation with path compression has amortized time complexity of α(N).Space Complexity Analysis:
Example in Python:
class Solution:
def equationsPossible(self, equations: List[str]) -> bool:
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
p = list(range(26)) # Initialize parent array
for e in equations:
if e[1] == '=': # Process equality equations
a, b = ord(e[0]) - ord('a'), ord(e[-1]) - ord('a')
p[find(a)] = find(b)
for e in equations:
if e[1] == '!': # Process inequality equations
a, b = ord(e[0]) - ord('a'), ord(e[-1]) - ord('a')
if find(a) == find(b):
return False # Inconsistent assignment
return True
The code in other languages (Java, C++, Go, Typescript) follows the same algorithm, with minor syntactic variations. They all achieve the same time and space complexity.