You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 <= equations.length <= 500
equations[i].length == 4
equations[i][0] and equations[i][3] are lowercase letters
Given an array equations of strings that represent relationships between variables, each string
equations[i]
has length4
and takes one of two different forms:"a==b"
or"a!=b"
. Here,a
andb
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:
Example 2:
Example 3:
Example 4:
Example 5:
Note:
1 <= equations.length <= 500
equations[i].length == 4
equations[i][0]
andequations[i][3]
are lowercase lettersequations[i][1]
is either'='
or'!'
equations[i][2]
is'='
这道题给了一系列简单的公式,某两个字母相等或者不等,然后问给的这些公式会不会产生矛盾,就比如说一个是
a==b
,另一个是a!=b
,这就是矛盾了。或者有些更复杂的,相等是可以传递的,比如例子4中,就是一种比较复杂的情况。这里比较直接的一种解法就是建立无向图来做,每个字母都可以当作一个结点,然后等号就表示相连的结点。开始时先跳过所有的不等式,通过所有的等式将这个图建立起来。然后再遍历所有的不等式,看这两个结点在图中是否相连,这里通过递归来检查两个结点是否相连,常规写法,注意要使用一个 HashSet 来标记已经访问过的结点,以免陷入死循环,参见代码如下:解法一:
这道题给的提示使用联合查找/并查集 Union Find,论坛上的高分解法也是清一色的 UF 做法。核心思想是初始时给每一个对象都赋上不同的标签,然后对于所有的等式,可以看作是属于同一个群组的对象,需要在 root 数组中标记一下,标记方法是两个对象分别调用 find 函数来找出祖先标签值,然后在 root 数组中再将这两个值连接起来。接下来遍历所有不等式,对不等号两端的对象分别调用 find 函数来找出祖先标签值,若相等了,则产生了矛盾了,因为这两个对象 suppose 不能属于同一个群组的,直接返回 false,参见代码如下:
解法二:
讨论:find 函数的写法其实有很多种,有递归形式,也有迭代形式的,可以参见博主之前的博客 Redundant Connection II。
Github 同步地址:
#990
类似题目:
Redundant Connection II
Friend Circles
Graph Valid Tree
参考资料:
https://leetcode.com/problems/satisfiability-of-equality-equations/
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/234474/C%2B%2B-7-lines-with-picture-union-find
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/234486/JavaC%2B%2BPython-Easy-Union-Find
LeetCode All in One 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: