LeetCode 990, Medium. Topics: Array, String, Union-Find, Graph Theory. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:
Problem
You are given an array of strings equations that represent relationships between variables where each string equations[i] is of length 4 and takes one of two different forms: x<sub>i</sub>==y<sub>i</sub> or x<sub>i</sub>!=y<sub>i</sub>. Here, x<sub>i</sub> and y<sub>i</sub> are lowercase letters (not necessarily different) that represent one-letter variable names.
Return true if it is possible to assign integers to variable names so as to satisfy all the given equations, or false otherwise.
Examples
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.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Last modified on September 7, 2026