LeetCode 990: Satisfiability of Equality Equations Solution

Master LeetCode problem 990 (Satisfiability of Equality Equations), a medium 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.

990. Satisfiability of Equality Equations

Problem Explanation

Explanation

To solve this problem, we can use a union-find (disjoint set) data structure. We will first go through all the equalities and union the variables that are equal. Then, we will go through all the inequalities and check if the variables are in the same set. If any pair of variables in the inequalities are in the same set, then the equations are not satisfiable.

Algorithm

  1. Initialize a parent array where each variable is its own parent.
  2. Iterate through all the equalities and union the variables that are equal.
  3. Iterate through all the inequalities and check if the variables are in the same set using the find function.
  4. If we find any pair of variables in the inequalities that are in the same set, return false.
  5. If we pass through all inequalities without any conflicts, return true.

Time Complexity

The time complexity of this algorithm is O(n + m * α(n)), where n is the number of equations and m is the number of inequalities. α(n) is the inverse Ackermann function and is essentially constant for all practical purposes.

Space Complexity

The space complexity is O(26) for storing the parent array and the rank array, where 26 represents the alphabet characters.

Solution Code

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

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 990 (Satisfiability of Equality Equations)?

This page provides optimized solutions for LeetCode problem 990 (Satisfiability of Equality 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 990 (Satisfiability of Equality Equations)?

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

Can I run code for LeetCode 990 on DevExCode?

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

Back to LeetCode Solutions