28 · Union-Find & Minimum Spanning Trees

#990 Satisfiability of Equality Equations

Mediumsolve on LeetCode ↗

frequently reported atAmazon

the problem

In plain words

Each equation is a 4-character string like "a==b" or "a!=b" over lowercase variables. Return true if you can assign integers to the variables so that every equation holds.

in ["a==b","b!=a"]

out false

in ["b==a","a==b"]

out true

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

out false

a, b, c are forced equal, so b != c is impossible.

  • 1 ≤ equations.length ≤ 500
  • equations[i].length == 4
  • equations[i][1] is '=' or '!'
  • equations[i][2] is '='

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #721
      Accounts Merge
      Mediumin Union-Find & MST
    2. #785
      Is Graph Bipartite?
      Mediumin Graphs