> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/graphs-bfs-and-dfs/satisfiability-of-equality-equations.md).

# Satisfiability of Equality Equations

Given an array equations of strings that represent relationships between variables, each string `equations[i]` has length `4` and takes one of two different forms: `"a==b"` or `"a!=b"`.  Here, `a` and `b` are lowercase letters (not necessarily different) that represent one-letter variable names.

Return `true` if and only if it is possible to assign integers to variable names so as to satisfy all the given equations.

**Example 1:**

```
Input: ["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.
```

**Example 2:**

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

**Example 3:**

```
Input: ["a==b","b==c","a==c"]
Output: true
```

**Example 4:**

```
Input: ["a==b","b!=c","c==a"]
Output: false
```

**Example 5:**

```
Input: ["c==c","b==d","x!=z"]
Output: true
```

**Note:**

1. `1 <= equations.length <= 500`
2. `equations[i].length == 4`
3. `equations[i][0]` and `equations[i][3]` are lowercase letters
4. `equations[i][1]` is either `'='` or `'!'`
5. `equations[i][2]` is `'='`

```java
class Solution {
    Map<Character, Character> parent;

    public boolean equationsPossible(String[] equations) {
        parent = new HashMap<>();
        for (String edge : equations) {
            char var1 = edge.charAt(0);
            char var2 = edge.charAt(3);
            if (!parent.containsKey(var1))
                parent.put(var1, var1);
            if (!parent.containsKey(var2))
                parent.put(var2, var2);
            if (edge.charAt(1) == '=')
                if (var1 != var2)
                    union(var1, var2);
        }
        for (String edge : equations) {
            if (edge.charAt(1) == '!') {
                char p1 = findParent(edge.charAt(0));
                char p2 = findParent(edge.charAt(3));
                if (p1 == p2)
                    return false;
            }
        }
        return true;
    }

    public void union(char v1, char v2) {
        char p1 = findParent(v1);
        char p2 = findParent(v2);
        if (p1 != p2)
            parent.put(p1, p2);
    }

    public char findParent(char c) {
        if (c != parent.get(c))
            parent.put(c, findParent(parent.get(c)));
        return parent.get(c);
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/graphs-bfs-and-dfs/satisfiability-of-equality-equations.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
