> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Satisfiability of Equality Equations

> Tested Python solution for LeetCode 990 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 990, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [String](/catalog/topics/string), [Union-Find](/catalog/topics/union-find), [Graph Theory](/catalog/topics/graph-theory). [View on LeetCode](https://leetcode.com/problems/satisfiability-of-equality-equations/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 990   # by problem number
lcpy gen -s satisfiability_of_equality_equations   # by problem name
```

## 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

```
Input: equations = ["a==b","b!=a"]
Output: false
Explanation: If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second.
There is no way to assign the variables to satisfy both equations.
```

```
Input: equations = ["b==a","a==b"]
Output: true
Explanation: We could assign a = 1 and b = 1 to satisfy both equations.
```

### 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/satisfiability_of_equality_equations/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/satisfiability_of_equality_equations/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * alpha(26))
    # Space: O(26)
    def equations_possible(self, equations: list[str]) -> bool:
        parent: list[int] = list(range(26))

        def find(x: int) -> int:
            while parent[x] != x:
                parent[x] = parent[parent[x]]
                x = parent[x]
            return x

        def union(a: int, b: int) -> None:
            root_a, root_b = find(a), find(b)
            if root_a != root_b:
                parent[root_a] = root_b

        for equation in equations:
            if equation[1] == "=":
                union(ord(equation[0]) - ord("a"), ord(equation[3]) - ord("a"))

        for equation in equations:
            if equation[1] == "!" and find(ord(equation[0]) - ord("a")) == find(
                ord(equation[3]) - ord("a")
            ):
                return False
        return True
```

## Complexity

| Time | Space |
| - | - |
| O(n \* alpha(26)) | O(26) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.