Skip to content

Latest commit

 

History

History

990

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Given an array equations of strings that represent relationships between variables, each string equations[i] has length 4 and takes one of two different forms: "a==b" or "a!=b".  Here, a and b are lowercase letters (not necessarily different) that represent one-letter variable names.

Return true if and only if it is possible to assign integers to variable names so as to satisfy all the given equations.

 

Example 1:

Input: ["a==b","b!=a"]
Output: false
Explanation: 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.

Example 2:

Input: ["b==a","a==b"]
Output: true
Explanation: We could assign a = 1 and b = 1 to satisfy both equations.

Example 3:

Input: ["a==b","b==c","a==c"]
Output: true

Example 4:

Input: ["a==b","b!=c","c==a"]
Output: false

Example 5:

Input: ["c==c","b==d","x!=z"]
Output: true

 

Note:

  1. 1 <= equations.length <= 500
  2. equations[i].length == 4
  3. equations[i][0] and equations[i][3] are lowercase letters
  4. equations[i][1] is either '=' or '!'
  5. equations[i][2] is '='

Related Topics:
Union Find, Graph

Solution 1. Union Find

// OJ: https://leetcode.com/problems/satisfiability-of-equality-equations
// Author: github.com/lzl124631x
// Time: O(Nlog26)
// Space: O(26)
class UnionFind {
    vector<int> id;
public:
    UnionFind(int n) : id(n) {
        iota(begin(id), end(id), 0);
    }
    int find(int a) {
        return id[a] == a ? a : (id[a] = find(id[a]));
    }
    void connect(int a, int b) {
        id[find(a)] = find(b);
    }
    bool connected(int a, int b) {
        return find(a) == find(b);
    }
};
class Solution {
public:
    bool equationsPossible(vector<string>& A) {
        UnionFind uf(26);
        for (auto &s : A) {
            if (s[1] == '=') uf.connect(s[0] - 'a', s[3] - 'a');
        }
        for (auto &s : A) {
            if (s[1] == '!' && uf.connected(s[0] - 'a', s[3] - 'a')) return false;
        }
        return true;
    }
};

Solution 2. Graph Coloring (DFS)

// OJ: https://leetcode.com/problems/satisfiability-of-equality-equations/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
    vector<bool> visited = vector<bool>(26, false);
    int color[26];
    vector<vector<int>> adj = vector<vector<int>>(26);
    void dfs(int u, int c) {
        visited[u] = true;
        color[u] = c;
        for (auto v : adj[u]) {
            if (!visited[v]) dfs(v, c);
        }
    }
public:
    bool equationsPossible(vector<string>& equations) {
        for (auto e : equations) {
            if (e[1] != '=') continue;
            adj[e[0] - 'a'].push_back(e[3] - 'a');
            adj[e[3] - 'a'].push_back(e[0] - 'a');
        }
        for (int i = 0, c = 0; i < 26; ++i) {
            if (!visited[i]) dfs(i, c++);
        }
        for (auto e : equations) {
            if (e[1] == '!' && color[e[0] - 'a'] == color[e[3] - 'a']) return false;
        }
        return true;
    }
};