Satisfiability of Equality Equations - Problem

Imagine you're a mathematician trying to solve a system of equations involving variables. You're given a collection of equality and inequality constraints between single-letter variables, and you need to determine if there's a way to assign integer values to these variables that satisfies all the given constraints simultaneously.

You are provided with an array of strings equations where each equation is exactly 4 characters long and follows one of two formats:

  • "xi==yi" - Variable xi must equal variable yi
  • "xi!=yi" - Variable xi must NOT equal variable yi

Here, xi and yi are lowercase letters representing variable names (they can be the same letter).

Your task: Return true if it's possible to assign integer values to all variables such that every equation is satisfied, or false if such an assignment is impossible.

For example, if you have ["a==b", "b==c", "a!=c"], this is impossible because if a equals b and b equals c, then a must equal c, which contradicts the third equation.

Input & Output

example_1.py โ€” Basic Equality Chain
$ Input: ["a==b","b==c","c==a"]
โ€บ Output: true
๐Ÿ’ก Note: All variables must be equal: a = b = c. We can assign any value (e.g., 1) to all variables to satisfy all equations.
example_2.py โ€” Contradiction Case
$ Input: ["a==b","b==c","a!=c"]
โ€บ Output: false
๐Ÿ’ก Note: If a==b and b==c, then a must equal c (transitivity). But a!=c creates a contradiction, making the system unsatisfiable.
example_3.py โ€” Independent Variables
$ Input: ["a==b","c==d","a!=c"]
โ€บ Output: true
๐Ÿ’ก Note: We have two separate groups: {a,b} and {c,d}. We can assign value 1 to a,b and value 2 to c,d, satisfying all constraints.

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

Visualization

Tap to expand
Equation Satisfiability with Union-FindExample: ["a==b", "b==c", "d!=a"]Phase 1: Process Equality ConstraintsInitial state:abcdAfter a==b:abcdAfter b==c:abcdPhase 2: Validate Inequality ConstraintsCheck constraint: d != aโ€ข find(d) = d (component representative)โ€ข find(a) = a (component representative, but in group {a,b,c})โœ“ Different components โ†’ Constraint satisfied!a,b,cgroupdโ‰ 
Understanding the Visualization
1
Parse Constraints
Separate equality (==) and inequality (!=) constraints
2
Build Groups
Use Union-Find to merge variables connected by equality
3
Validate
Check that no inequality constraint exists within same group
Key Takeaway
๐ŸŽฏ Key Insight: Union-Find efficiently handles transitivity of equality while allowing quick component membership checks for inequality validation.
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
28.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen