-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSatisfiability of Equality Equations.cpp
55 lines (40 loc) · 1.51 KB
/
Satisfiability of Equality Equations.cpp
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
51
52
53
54
55
/*
Solution by Rahul Surana
***********************************************************
You are given an array of strings equations that represent relationships between variables
where each string equations[i] is of length 4 and takes one of two different forms: "xi==yi" or "xi!=yi".
Here, xi and yi are lowercase letters (not necessarily different) that represent one-letter variable names.
Return true if it is possible to assign integers to variable names so as to satisfy all the given equations, or false otherwise.
***********************************************************
*/
class Solution {
public:
map<char,char> p;
int get_p(char a){
if(a == p[a]) return a;
return p[a] = get_p(p[a]);
}
bool equationsPossible(vector<string>& e) {
for(int i = 0; i < e.size(); i++){
if(p.find(e[i][0]) == p.end()){
p[e[i][0]] = e[i][0];
}
if(p.find(e[i][3]) == p.end()){
p[e[i][3]] = e[i][3];
}
if(e[i][1] == '='){
char a = get_p(e[i][0]);
char b = get_p(e[i][3]);
p[a] = b;
}
}
for(int i = 0; i < e.size(); i++){
if(e[i][1] == '!'){
char a = get_p(e[i][0]);
char b = get_p(e[i][3]);
if(a == b) return false;
}
}
return true;
}
};