Satisfiability of Equality Equations

Given a list of equality and inequality equations over lowercase letters, determine whether all equations can be satisfied simultaneously. DSU union by rank is a clean way to merge equal variables before checking conflicts.

Input Format

equations = array of equality / inequality strings

Output Format

whether all equations are satisfiable

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • Input must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

equations = ["a==b","b!=c","c==a"]

Output:

false

Explanation:

After merging a, b, and c through equalities, the inequality b!=c cannot hold.

Example 2:

Input:

equations = ["a==b","b==c","a==c"]

Output:

true

Explanation:

All equalities are compatible.

Example 3:

Input:

equations = ["c==c","b==d","x!=z"]

Output:

true

Explanation:

Trivial equalities and unrelated inequalities are satisfiable.

Loading...
Satisfiability of Equality Equations