<p>You are given an array of strings <code>equations</code> that represent relationships between variables where each string <code>equations[i]</code> is of length <code>4</code> and takes one of two different forms: <code>&quot;x<sub>i</sub>==y<sub>i</sub>&quot;</code> or <code>&quot;x<sub>i</sub>!=y<sub>i</sub>&quot;</code>.Here, <code>x<sub>i</sub></code> and <code>y<sub>i</sub></code> are lowercase letters (not necessarily different) that represent one-letter variable names.</p>

<p>Return <code>true</code><em> if it is possible to assign integers to variable names so as to satisfy all the given equations, or </em><code>false</code><em> otherwise</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> equations = [&quot;a==b&quot;,&quot;b!=a&quot;]
<strong>Output:</strong> false
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> equations = [&quot;b==a&quot;,&quot;a==b&quot;]
<strong>Output:</strong> true
<strong>Explanation:</strong> We could assign a = 1 and b = 1 to satisfy both equations.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= equations.length &lt;= 500</code></li>
	<li><code>equations[i].length == 4</code></li>
	<li><code>equations[i][0]</code> is a lowercase letter.</li>
	<li><code>equations[i][1]</code> is either <code>&#39;=&#39;</code> or <code>&#39;!&#39;</code>.</li>
	<li><code>equations[i][2]</code> is <code>&#39;=&#39;</code>.</li>
	<li><code>equations[i][3]</code> is a lowercase letter.</li>
</ul>

class Solution {
public:
    int parent[26];
    int rank[26];
    int find(int x)
    {
        if(x!=parent[x])
            return parent[x]=find(parent[x]);
        return x;
    }
    void unions_ops(int x,int y)
    {
        int parx=find(x);
        int pary=find(y);
        if(parx!=pary)//means they are in different set
        {
            if(rank[parx]>rank[pary])
            {
                parent[pary]=parx;
            }
            else if(rank[parx]<rank[pary])
            {
                parent[parx]=pary;
            }
            else
            {
                //any become parent and parents's rank is increment by one
                parent[parx]=pary;
                rank[pary]++;
            }
        }
    }
    bool equationsPossible(vector<string>& equations) {
        for(int i=0;i<26;i++)
        {
            parent[i]=i;
            rank[i]=0;
        }
        for(auto x:equations)
        {
           if(x[1]=='=')
           {
                unions_ops(x[0]-'a',x[3]-'a');
           }
        }
        for(auto x:equations)
        {
            if(x[1]=='!' && find(x[0]-'a')==find(x[3]-'a'))
            {
                    return false;
            }
        }
        return true;
    }
};


/*
Yes, exactly! If two nodes (or variables) are in the same set, it means they are connected by a series of "==" relationships. As a result, they must be equal to each other.

### Understanding with More Clarity

#### Core Idea
1. **Same Set (Connected Component):**  
   When two nodes are in the same set, they are part of a single connected component formed by "==" relationships. This means, directly or indirectly, these two nodes are equal to each other.

2. **Violation of Inequality (`!=`):**  
   If an equation states "x != y", but both `x` and `y` are found to be in the same set (i.e., `find(x) == find(y)`), it causes a contradiction:
   - **Why?** Because "x != y" is saying that `x` and `y` should not be equal, but since they are in the same set, they are equal.

#### Example to Illustrate

Let's use an example to clarify:

```plaintext
Input: equations = ["a==b", "b==c", "a!=c"]
```

**Step-by-Step Execution:**

1. **Process Equality Equations:**
   - "a == b":  
     `union1('a', 'b')` connects `a` and `b` in the same set.  
     Set becomes: `{a, b}`
   - "b == c":  
     `union1('b', 'c')` connects `b` and `c` in the same set.  
     Set becomes: `{a, b, c}`  

   Now, all three (`a, b, c`) are in the same set, meaning they are all equal to each other.

2. **Process Inequality Equations:**
   - "a != c":  
     Check if `find('a') == find('c')`:  
     Both `a` and `c` are in the same set (`find('a') == find('c')`).  
     This means `a` and `c` are equal to each other.  

   **Contradiction:**  
   The equation "a != c" states `a` and `c` should not be equal, but we know they are in the same set and hence are equal.  
   This contradiction leads to returning `false`.

### Conclusion

- **Key Insight:** If two nodes are in the same set, they represent variables that are equal to each other.
- Therefore, any `!=` equation involving these nodes would be violated (i.e., false), as it contradicts the fact that they are considered equal due to being in the same set.
*/
14800