<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:
    bool equationsPossible(vector<string>& equations) {
        return helper2(equations);
    }

    bool helper2(vector<string>& equations)
    {
        unordered_map<char, vector<char>> eqMap;
       // unordered_map<char, bool> seen;

        for (auto equation : equations)
        {
            char from = equation[0];
            char to = equation[3];
            char op1 = equation[1];
            char op2 = equation[2];

            if (op1 == op2)
            {
                eqMap[from].push_back(to);
                eqMap[to].push_back(from);
            }
        }

        for (auto equation: equations)
        {
            char from = equation[0];
            char to = equation[3];
            char op1 = equation[1];
            char op2 = equation[2];

            if (op1 == op2)
            {
                continue;
            }
            
            unordered_map<char, bool> seen;
            auto ret = bfs2(from, to, seen, eqMap);

            if (!ret)
            {
                return ret;
            }
        }

        return true;
    }

    bool bfs2(char from, char to, unordered_map<char, bool>& seen, unordered_map<char, vector<char>>& eqMap)
    {
        if (from == to)
        {
            return false;
        }
        seen[from] = 1;
        auto adjNodes = eqMap[from];

        for (auto node : adjNodes)
        {
            if (seen[node])
            {
                continue;
            }

            if (node == to)
            {
                return false;
            }

            auto ret = bfs2(node, to, seen, eqMap);

            if (!ret)
            {
                return ret;
            }
        }

        return true;    
    }

    bool helper1(vector<string>& equations)
    {
        unordered_map<char, vector<char>> eqMap;

         for (auto equation : equations)
        {
            char n1 = equation[0];
            char n2 = equation[3];
            char op1 = equation[1];
            char op2 = equation[2];

            if (op1 == '=')
            {
                if (n1 == n2)
                {
                    continue;
                }
                eqMap[n1].push_back(n2);
                eqMap[n2].push_back(n1);
            }
        }

        for (auto equation : equations)
        {
            unordered_map<char, bool> seen;
            char n1 = equation[0];
            char n2 = equation[3];
            char op1 = equation[1];
            char op2 = equation[2];

            if (op1 == '!')
            {
                int a = 1;
            }

            if (n1 != n2 && (eqMap.count(n1) == 0 || eqMap.count(n2) == 0))
            {
                continue;
            }
            bool flag = 0;
            auto ret = dfs1(eqMap, n1, n2, op1, seen, flag);
            cout << n1 << " " << n2 << " " << op1 << " " << ret <<endl;
            if (!ret)
            {
                if (!flag && op1 == '!')
                {
                   // return true;
                }
                return false;
            }
        }

        return true;
    }

    bool dfs1(unordered_map<char,vector<char>>& eqMap, char from, char to, char op,  
    unordered_map<char, bool>& seen, bool& flag)
    {
        seen[from] = 1;

        if (from == to)
        {
            if (op == '!')
            {
                flag = 1;
                return false;
            }
            return true;
        }

        auto neighbors = eqMap[from];

        if (find(neighbors.begin(), neighbors.end(), to) != neighbors.end())
        {
            if (op == '=')
            {
                return true;
            }
            else
            {
                flag = 1;
            return false;
            }
        }
       

        for (auto neighbor : neighbors)
        {
            if (seen[neighbor])
            {   
                continue;
            }

            if (neighbor == to)
            {
                if (op == '=')
                return true;
                else
                {
                    flag = 1;
                return false;
                }
            }

            bool ret = dfs1(eqMap, neighbor, to, op, seen, flag);

            if (ret)
            {
                return ret;
            }
        }

        return false;
    }
};
17300