Skip to content

Commit 7ad5f54

Browse files
authored
Merge pull request #16 from mrudul0026/main
LC-990 Solution
2 parents f913521 + 4e1cb40 commit 7ad5f54

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Satisfiability of Equality Equations
3+
4+
You are given an array of strings equations that represent relationships between
5+
variables where each string equations[i] is of length 4 and takes one of
6+
two different forms: "xi==yi" or "xi!=yi".Here, xi and yi are lowercase letters
7+
(not necessarily different) that represent one-letter variable names.
8+
9+
Return true if it is possible to assign integers to variable names so as
10+
to satisfy all the given equations, or false otherwise.
11+
12+
Input: equations = ["a==b","b!=a"]
13+
Output: false
14+
Explanation: If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second.
15+
There is no way to assign the variables to satisfy both equations.
16+
17+
Input: equations = ["b==a","a==b"]
18+
Output: true
19+
Explanation: We could assign a = 1 and b = 1 to satisfy both equations.
20+
21+
Input: equations = ["a==b","b==c","a==c"]
22+
Output: true
23+
24+
Input: equations = ["a==b","b!=c","c==a"]
25+
Output: false
26+
27+
Input: equations = ["c==c","b==d","x!=z"]
28+
Output: true
29+
30+
Constraints:
31+
1 <= equations.length <= 500
32+
equations[i].length == 4
33+
equations[i][0] is a lowercase letter.
34+
equations[i][1] is either '=' or '!'.
35+
equations[i][2] is '='.
36+
equations[i][3] is a lowercase letter.
37+
38+
*/
39+
40+
/*
41+
Idea:
42+
Since only atmost 2 variables are involved in an equation and the equation can only
43+
be == or !=, the only possible scenerio where unsatisfiability takes place is when
44+
two variables which were declared directly or indirectly equal is declared unequal.
45+
46+
So we will first process all the equalities first. for every equality we will indicate it
47+
by an edge and any two variables directly/indirectly connected by edge means they are equal
48+
this can by captured using union operation because if 2 nodes have indirect/direct connection
49+
they will yield the same parent/component when doing find.
50+
51+
Now we will process the inequalities. Here we will just check if find is same then return false
52+
because same component variables are equal but now contradiction is happening and hence unsatisfiable.
53+
54+
If no contradiction after processing everything, then return true
55+
56+
Time Complexity: O(N) where N is equations.length
57+
Space Complexity: O(26) = O(1)
58+
*/
59+
60+
61+
class Solution {
62+
public:
63+
vector<int> p,sz;
64+
int find(int a){
65+
if(p[a]!=a)p[a]=find(p[a]);
66+
return p[a];
67+
}
68+
void unite(int a ,int b){
69+
a=find(a);
70+
b=find(b);
71+
p[a]=p[b];
72+
73+
}
74+
bool equationsPossible(vector<string>& equations) {
75+
p.resize(26),sz.resize(26,1);
76+
for(int i=0;i<26;i++)p[i]=i;
77+
for(auto &s: equations){
78+
if(s[1]=='=')unite(s[0]-'a',s[3]-'a');
79+
}
80+
for(auto &s: equations){
81+
if(s[1]!='='){
82+
if(find(s[0]-'a')==find(s[3]-'a'))return false;
83+
}
84+
}
85+
return true;
86+
}
87+
};
Loading

0 commit comments

Comments
 (0)