<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:
    struct comp {
    bool operator() (const string &a,const string &b) const {
        if(a[0] < b[0]) return true;
        return false;  // Change this to define your custom comparison logic
    }
};
    int find1(int x,vector<int>&parent) {
        if(x == parent[x]) 
        {
            return x;
        }
        return parent[x] = find1(parent[x],parent);
    }
    
    void union1(int x,int y,vector<int>&parent,vector<int>&rank) 
    {
        int x_parent = find1(x,parent);
        int y_parent = find1(y,parent);
        if(x_parent == y_parent) return;
        if(rank[x_parent] > rank[y_parent]) {
            parent[y_parent] = x_parent;
        }
        else if(rank[x_parent] < rank[y_parent]) {
            parent[x_parent] = y_parent;
        }
        else {
            parent[x_parent] = y_parent;
            rank[y_parent]+=1;
        }
    }
    bool equationsPossible(vector<string>& equations) {
        sort(equations.begin(),equations.end());
        vector<int>parent(26);
        for(int i = 0;i<26;i++) {
            parent[i] = i;
        }
        vector<int>rank(26,0);
        vector<string>extra;
        for(int i = 0;i<equations.size();i++) 
        {
            char v1,v2;string op;
            for(int j = 0;j<equations[i].size();j++) {
                if(j == 0) v1 = equations[i][j];
                else if(j == 1 || j == 2) op.push_back(equations[i][j]);
                else if(j == 3) {
                    v2 = equations[i][j];
                }
            }
            if(op == "==") {
                union1(v1 - 'a',v2 - 'a',parent,rank);
            }
            else if(op == "!=" ){
                extra.push_back(equations[i]);
                continue;
            }
        }
       
        for(int i = 0;i<extra.size();i++) 
        {
            char v1,v2;string op;
            for(int j = 0;j<extra[i].size();j++) {
                if(j == 0) v1 = extra[i][j];
                else if(j == 1 || j == 2) op.push_back(extra[i][j]);
                else if(j == 3) {
                    v2 = extra[i][j];
                }
            }
            if(parent[v1-'a'] == parent[v2-'a']) return false;
        }
        for(auto it:parent) 
        {
            cout << it <<  " ";
        }
        return true;
    }
};
16100