mediumUnion FindStringUnion Find

Satisfiability of Equality Equations

mediumTime: O(n * α(26))Space: O(1)

Recognize the pattern

equality and inequality constraintsa==b means same groupa!=b means different groups

Brute force idea

The naive version of Satisfiability of Equality Equations sounds like this: Try all possible value assignments — exponential. Each variable could take any value. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.

Better approach

The real unlock in Satisfiability of Equality Equations comes when you notice this: Union-Find: first, process all equalities (union a and b). Then check all inequalities — if a!=b but find(a)==find(b), it's unsatisfiable. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

Key invariant

The compass for Satisfiability of Equality Equations is this: Equality is transitive (a==b, b==c ⟹ a==c), making Union-Find natural. After merging all equal variables, any inequality between two variables in the same set is a contradiction. As long as that statement keeps holding, you can trust the steps built on top of it.

Watch out for

One easy way to drift off course in Satisfiability of Equality Equations is this: Processing inequalities before equalities — equalities must be processed first because they constrain which variables must be equal. Then inequalities check for contradictions. The fix is usually to return to the meaning of each move, not just the steps themselves.

Union Find Pattern