<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 UnionFind {
private:
    vector< int > root;
    vector< int > rank;

public:
    UnionFind( int size ) : root( size ), rank( size ) {
        for( int i = 0; i < size; i++ ) {
            root[ i ] = i;
            rank[ i ] = 1;
        }
    }

    int find( int x ) {
        if( root[ x ] == x ) {
            return x;
        }
        root[ x ] = find( root[ x ] );
        return root[ x ];
    }

    void doUnion( int x, int y ) {
        int rootX = find( x );
        int rootY = find( y );
        if( rootX != rootY ) {
            if( rank[ rootX ] > rank[ rootY ] ) {
                root[ rootY ] = rootX;
            } else if( rank[ rootY ] > rank[ rootX ] ) {
                root[ rootX ] = rootY;
            } else {
                root[ rootY ] = rootX;
                rank[ rootX ]++;
            }
        }
    }

    bool isConnected( int x, int y ) {
        return find( x ) == find( y );
    }
};

class Solution {
public:
    static bool compare( string a, string b ) {
        if( a[ 1 ] != b[ 1 ] ) {
            return a[ 1 ] == '!';
        }
        return true;
    }

    bool equationsPossible(vector<string>& equations) {
        int numEquations = equations.size();

        vector< string > sortedEquations( numEquations );
        int start = 0, end = numEquations-1;

        unordered_map< char, int > variables;
        int count = 0;

        // First shift all the equations with == to the front and union their variables( make a set of connected vertices )
        for( const auto& equation : equations ) {
            if( equation[ 1 ] == '=' ) {
                sortedEquations[ start++ ] = equation;
            } else {
                sortedEquations[ end-- ] = equation;
            }
            if( variables.find( equation[ 0 ] ) == variables.end() ) {
                variables.insert( { equation[ 0 ], count++ } );
            }
            if( variables.find( equation[ 3 ] ) == variables.end() ) {
                variables.insert( { equation[ 3 ], count++ } );
            }
        }
        
        UnionFind uf( count );
        
        for( const auto& equation : sortedEquations ) {
            if( equation[ 1 ] == '=' ) {
                uf.doUnion( variables[ equation[ 0 ] ], variables[ equation[ 3 ] ] );
            } else {
                if( uf.isConnected( variables[ equation[ 0 ] ], variables[ equation[ 3 ] ] ) ) {
                    return false;
                }
            }
        }

        return true;
    }
};
16000