forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2.cpp
More file actions
21 lines (21 loc) · 638 Bytes
/
s2.cpp
File metadata and controls
21 lines (21 loc) · 638 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// OJ: https://leetcode.com/problems/satisfiability-of-equality-equations/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
int uf[26];
int find(int x) {
return uf[x] == x ? x : (uf[x] = find(uf[x]));
}
public:
bool equationsPossible(vector<string>& equations) {
for (int i = 0; i < 26; ++i) uf[i] = i;
for (auto e : equations) {
if (e[1] == '=') uf[find(e[0] - 'a')] = find(e[3] - 'a');
}
for (auto e : equations) {
if (e[1] == '!' && find(e[0] - 'a') == find(e[3] - 'a')) return false;
}
return true;
}
};