-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC - 990. Satisfiability of Equality Equations
50 lines (48 loc) · 1.35 KB
/
LC - 990. Satisfiability of Equality Equations
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public:
void graphColor(vector<vector<int>>& adjL, vector<int>& color, int node, int rang)
{
color[node] = rang;
for(auto child : adjL[node])
{
if(color[child] == -1) graphColor(adjL, color, child, rang);
}
}
bool equationsPossible(vector<string>& eqn)
{
vector<vector<int>> adjL(26);
for(int i = 0; i < eqn.size(); i++)
{
if(eqn[i][1] == '=')
{
adjL[eqn[i][0]-'a'].push_back(eqn[i][3]-'a');
if(eqn[i][0] != eqn[i][3])
{
adjL[eqn[i][3]-'a'].push_back(eqn[i][0] - 'a');
}
}
}
vector<int> color(26,-1);
int rang = 0;
for(int i = 0; i < 26; i++)
{
if(adjL[i].size() != 0 && color[i] == -1)
{
cout << i << endl;
rang = rang + 1;
graphColor(adjL, color, i, rang);
}
}
for(int i = 0; i < eqn.size(); i++)
{
if(eqn[i][1] == '!')
{
if( (eqn[i][0] == eqn[i][3]) || color[eqn[i][0]-'a'] != -1 && (color[eqn[i][0]-'a'] == color[eqn[i][3] - 'a']))
{
return false;
}
}
}
return true;
}
};