990. Satisfiability of Equality Equations

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 '='

Rust Solution

struct Solution;

struct UnionFind {
    parent: Vec<usize>,
    n: usize,
}

impl UnionFind {
    fn new(n: usize) -> Self {
        let parent = (0..n).collect();
        UnionFind { parent, n }
    }

    fn find(&mut self, i: usize) -> usize {
        let j = self.parent[i];
        if i == j {
            j
        } else {
            let k = self.find(j);
            self.parent[i] = k;
            k
        }
    }

    fn union(&mut self, i: usize, j: usize) {
        let i = self.find(i);
        let j = self.find(j);
        if i != j {
            self.parent[i] = j;
            self.n -= 1;
        }
    }
}

impl Solution {
    fn equations_possible(equations: Vec<String>) -> bool {
        let mut uf = UnionFind::new(26);
        let mut pairs: Vec<(usize, usize)> = vec![];
        for equation in equations {
            let s: Vec<char> = equation.chars().collect();
            let i = (s[0] as u8 - b'a') as usize;
            let j = (s[3] as u8 - b'a') as usize;
            if s[1] == '=' {
                uf.union(i, j);
            } else {
                pairs.push((i, j));
            }
        }
        for pair in pairs {
            let i = pair.0;
            let j = pair.1;
            if uf.find(i) == uf.find(j) {
                return false;
            }
        }
        true
    }
}

#[test]
fn test() {
    let equations = vec_string!["a==b", "b!=a"];
    let res = false;
    assert_eq!(Solution::equations_possible(equations), res);
    let equations = vec_string!["b==a", "a==b"];
    let res = true;
    assert_eq!(Solution::equations_possible(equations), res);
    let equations = vec_string!["a==b", "b==c", "a==c"];
    let res = true;
    assert_eq!(Solution::equations_possible(equations), res);
    let equations = vec_string!["a==b", "b!=c", "c==a"];
    let res = false;
    assert_eq!(Solution::equations_possible(equations), res);
    let equations = vec_string!["c==c", "b==d", "x!=z"];
    let res = true;
    assert_eq!(Solution::equations_possible(equations), res);
}

Having problems with this solution? Click here to submit an issue on github.