Satisfiability of Equality Equations

Medium#990

You get equations of the form "p==q" or "p!=q", where p and q are lowercase letters standing for variables. Say whether the letters can be given values so that every equation holds. Our example: "a==b", "d!=b", "c!=a", "b==c" → false.

input
equations: equalities "p==q" and inequalities "p!=q", where p and q are lowercase letters standing for variables.
returns
true if the letters can be given values so that every equation holds; otherwise false.
constraints
  • 1 ≤ equations.length ≤ 500
  • equations[i] is 4 characters: a lowercase letter, '==' or '!=', a lowercase letter
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {string[]} equations
 * @return {boolean}
 */
function equationsPossible(equations) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more union-find problems

Pattern “Union-Find” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.