1032. Satisfiability of Equality Equations
My accepted Python solution to LeetCode problem 1032, Satisfiability of Equality Equations, running in 0ms.
- Difficulty: Medium
- Python
- Runtime 0ms
- Memory 17.6MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 0ms, memory 17.6MB, accepted 2026-01-02.
class Solution:
def equationsPossible(self, equations: List[str]) -> bool:
# Union-Find
parent = list(range(26))
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
def union(x, y):
px, py = find(x), find(y)
if px != py:
parent[px] = py
# First pass: process all equalities
for eq in equations:
if eq[1] == '=':
x = ord(eq[0]) - ord('a')
y = ord(eq[3]) - ord('a')
union(x, y)
# Second pass: check inequalities
for eq in equations:
if eq[1] == '!':
x = ord(eq[0]) - ord('a')
y = ord(eq[3]) - ord('a')
if find(x) == find(y):
return False
return True