<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:
    def __init__(self):
        self.parent = []
        self.rank =[]

    def find(self,i):

        if self.parent[i] == i : return i

        self.parent[i] = self.find(self.parent[i])
        
        return self.parent[i]

    def union(self,x,y):
        x_p = self.find(x)
        y_p = self.find(y)

        if x_p == y_p : return 

        if self.rank[x_p] > self.rank[y_p]:
            self.parent[y_p] = x_p

        elif self.rank[x_p] < self.rank[y_p]:
            self.parent[x_p] = y_p

        else:
            ## if both the parent are similar then update rank of one and assign parent 
            self.parent[x_p] = y_p
            self.rank[y_p] += 1
        
    
    def equationsPossible(self, equations: List[str]) -> bool:
        self.parent = list(range(26))
        self.rank = [1] * 26

        for eq in equations:
            if eq[1] == "=":
                ## find the parent and union 
                x = (ord(eq[0])- ord('a'))
                y = (ord(eq[3])- ord('a'))
                self.union (x,y)

        for eq in equations:
            if eq[1] == "!":
                # compare the parent and return 
                x = self.find(ord(eq[0])- ord('a'))
                y = self.find(ord(eq[3])- ord('a'))

                if self.find(x) == self.find(y):
                    return False # as both of them should be same 


        return True 


        
16500