-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path990.cpp
More file actions
27 lines (25 loc) · 669 Bytes
/
Copy path990.cpp
File metadata and controls
27 lines (25 loc) · 669 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
bool equationsPossible(vector<string> &equations) {
match = vector<int>(26, 0);
for (int i = 0; i < 26; ++i)
match[i] = i;
for (auto &s:equations) {
if (s[1] == '=')
match[Match(s[0] - 'a')] = Match(s[3] - 'a');
}
for (auto &s:equations) {
if (s[1] == '!')
if (Match(match[s[0] - 'a']) == Match(match[s[3] - 'a']))
return false;
}
return true;
}
private:
vector<int> match;
int Match(int i) {
if (match[i] == i)
return i;
return Match(match[i]);
}
};