forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3.cpp
More file actions
31 lines (31 loc) · 953 Bytes
/
s3.cpp
File metadata and controls
31 lines (31 loc) · 953 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
28
29
30
31
// OJ: https://leetcode.com/problems/satisfiability-of-equality-equations/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
vector<bool> visited = vector<bool>(26, false);
int color[26];
vector<vector<int>> adj = vector<vector<int>>(26);
void dfs(int u, int c) {
visited[u] = true;
color[u] = c;
for (auto v : adj[u]) {
if (!visited[v]) dfs(v, c);
}
}
public:
bool equationsPossible(vector<string>& equations) {
for (auto e : equations) {
if (e[1] != '=') continue;
adj[e[0] - 'a'].push_back(e[3] - 'a');
adj[e[3] - 'a'].push_back(e[0] - 'a');
}
for (int i = 0, c = 0; i < 26; ++i) {
if (!visited[i]) dfs(i, c++);
}
for (auto e : equations) {
if (e[1] == '!' && color[e[0] - 'a'] == color[e[3] - 'a']) return false;
}
return true;
}
};