[LeetCode] 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 '='

等式方程的可满足性。

给定一个由表示变量之间关系的字符串方程组成的数组,每个字符串方程 equations[i] 的长度为 4,并采用两种不同的形式之一:"a==b" 或 "a!=b"。在这里,a 和 b 是小写字母(不一定不同),表示单字母变量名。

只有当可以将整数分配给变量名,以便满足所有给定的方程时才返回 true,否则返回 false。 

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/satisfiability-of-equality-equations
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是并查集union find。如果中间是等号,说明等号两边的内容应该是同组的,反之则是不同组的。先扫描是等号的,将同组的东西都组合起来;然后扫描不等号的两边,看看是否有跟已经union好的内容有矛盾的地方。

时间O(nlogn)

空间O(n)

Java实现

 1 class Solution {
 2     public boolean equationsPossible(String[] equations) {
 3         UF uf = new UF(256);
 4         for (String e : equations) {
 5             int letter1 = e.charAt(0) - 'a';
 6             int letter2 = e.charAt(3) - 'a';
 7             if (e.charAt(1) == '=') {
 8                 uf.union(letter1, letter2);
 9             }
10         }
11 
12         for (String e : equations) {
13             if (e.charAt(1) == '!') {
14                 int letter1 = e.charAt(0) - 'a';
15                 int letter2 = e.charAt(3) - 'a';
16                 if (uf.find(letter1) == uf.find(letter2)) {
17                     return false;
18                 }
19             }
20         }
21         return true;
22     }
23 }
24 
25 class UF {
26     int[] parent;
27     int count;
28 
29     public UF(int n) {
30         parent = new int[n];
31         for (int i = 0; i < n; i++) {
32             parent[i] = i;
33         }
34         count = n;
35     }
36 
37     public int find(int i) {
38         if (i == parent[i]) {
39             return i;
40         }
41         return parent[i] = find(parent[i]);
42     }
43 
44     public void union(int a, int b) {
45         int aRoot = find(a);
46         int bRoot = find(b);
47         if (aRoot != bRoot) {
48             parent[aRoot] = bRoot;
49             count--;
50         }
51     }
52 }

 

LeetCode 题目总结

posted @ 2020-06-08 05:42  CNoodle  阅读(150)  评论(0编辑  收藏  举报