1
class Solution {
2
int uf[26];
3
int find(int x) {
4
return uf[x] == x ? x : (uf[x] = find(uf[x]));
5
}
6

7
public:
8
bool equationsPossible(vector<string> &equations) {
9
for (int i = 0; i < 26; ++i) uf[i] = i;
10
for (auto e : equations) {
11
if (e[1] == '=') uf[find(e[0] - 'a')] = find(e[3] - 'a');
12
}
13
for (auto e : equations) {
14
if (e[1] == '!' && find(e[0] - 'a') == find(e[3] - 'a')) return false;
15
}
16
return true;
17
}
18
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0