LeetCode 2307: Check for Contradictions in Equations Solution

Master LeetCode problem 2307 (Check for Contradictions in Equations), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2307. Check for Contradictions in Equations

Problem Explanation

Explanation

To solve this problem, we can use the Union Find algorithm. We will first iterate through all the equations and build the union-find data structure based on the equalities. Then, we will iterate through all the inequalities and check if the two variables are connected in the union-find. If they are connected, it means there is a contradiction in the equations.

Algorithm:

  1. Create a Union Find data structure.
  2. Iterate through all the equations and union the variables if they are equal.
  3. Iterate through all the inequalities and check if the two variables are connected in the union-find data structure.
  4. If they are connected, return false (contradiction).
  5. If no contradictions are found, return true.

Time Complexity:

The time complexity of this algorithm is O(n + m * log(n)), where n is the number of variables and m is the total number of equations and inequalities.

Space Complexity:

The space complexity is O(n), where n is the number of variables.

Solution Code

class Solution {
    public boolean equationsPossible(String[] equations) {
        UnionFind uf = new UnionFind(26);
        
        for (String eq : equations) {
            if (eq.charAt(1) == '=') {
                int x = eq.charAt(0) - 'a';
                int y = eq.charAt(3) - 'a';
                uf.union(x, y);
            }
        }
        
        for (String eq : equations) {
            if (eq.charAt(1) == '!') {
                int x = eq.charAt(0) - 'a';
                int y = eq.charAt(3) - 'a';
                if (uf.find(x) == uf.find(y)) {
                    return false;
                }
            }
        }
        
        return true;
    }
    
    class UnionFind {
        int[] parent;
        
        public UnionFind(int n) {
            parent = new int[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
            }
        }
        
        public void union(int a, int b) {
            int rootA = find(a);
            int rootB = find(b);
            parent[rootA] = rootB;
        }
        
        public int find(int a) {
            if (parent[a] != a) {
                parent[a] = find(parent[a]);
            }
            return parent[a];
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2307 (Check for Contradictions in Equations)?

This page provides optimized solutions for LeetCode problem 2307 (Check for Contradictions in Equations) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2307 (Check for Contradictions in Equations)?

The time complexity for LeetCode 2307 (Check for Contradictions in Equations) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2307 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2307 in Java, C++, or Python.

Back to LeetCode Solutions