Skip to content

Commit a1633b8

Browse files
authored
Merge pull request #4055 from sreevidya-16/main
Add solution to LC problem 990
2 parents aa7dd8a + 02b5ae4 commit a1633b8

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
id: satisfiability-of-equality-equations
3+
title: Satisfiability of Equality Equations
4+
sidebar_label: Satisfiability of Equality Equations
5+
tags: [Graph, Union Find, Array, String, C++, Python, Java]
6+
description: Determine if it is possible to assign integers to variable names to satisfy all given equality and inequality equations.
7+
---
8+
9+
## Problem Statement
10+
11+
### Problem Description
12+
13+
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.
14+
15+
Return `true` if it is possible to assign integers to variable names so as to satisfy all the given equations, or `false` otherwise.
16+
17+
### Example
18+
19+
**Example 1:**
20+
```
21+
Input: equations = ["a==b","b!=a"]
22+
Output: false
23+
```
24+
**Explanation:** If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second. There is no way to assign the variables to satisfy both equations.
25+
26+
27+
### Constraints
28+
29+
- $1 \leq \text{equations.length} \leq 500$
30+
- `equations[i].length == 4`
31+
- `equations[i][0]` is a lowercase letter.
32+
- `equations[i][1]` is either `'='` or `'!'`.
33+
- `equations[i][2]` is `'='`.
34+
- `equations[i][3]` is a lowercase letter.
35+
36+
## Solution
37+
38+
### Intuition
39+
40+
The problem can be solved using Union-Find (Disjoint Set Union) data structure. The idea is to first process all equality equations to form connected components (sets of variables that must be equal). Then, we check all inequality equations to ensure that no two variables in an inequality equation belong to the same connected component.
41+
42+
### Time Complexity and Space Complexity Analysis
43+
44+
- **Time Complexity**: $O(n \log n)$, where $n$ is the number of equations. The Union-Find operations (union and find) are almost constant time due to path compression and union by rank.
45+
- **Space Complexity**: $O(1)$, aside from the space used for storing the equations and Union-Find data structure.
46+
47+
### Code
48+
49+
#### C++
50+
51+
```cpp
52+
class Solution {
53+
public:
54+
bool equationsPossible(vector<string>& equations) {
55+
vector<int> parent(26);
56+
iota(parent.begin(), parent.end(), 0);
57+
58+
function<int(int)> find = [&](int x) {
59+
return parent[x] == x ? x : parent[x] = find(parent[x]);
60+
};
61+
62+
for (const auto& eq : equations) {
63+
if (eq[1] == '=') {
64+
parent[find(eq[0] - 'a')] = find(eq[3] - 'a');
65+
}
66+
}
67+
68+
for (const auto& eq : equations) {
69+
if (eq[1] == '!' && find(eq[0] - 'a') == find(eq[3] - 'a')) {
70+
return false;
71+
}
72+
}
73+
```
74+
#### Python
75+
```python
76+
class Solution:
77+
def equationsPossible(self, equations: List[str]) -> bool:
78+
parent = list(range(26))
79+
80+
def find(x):
81+
if parent[x] != x:
82+
parent[x] = find(parent[x])
83+
return parent[x]
84+
85+
for eq in equations:
86+
if eq[1] == '=':
87+
parent[find(ord(eq[0]) - ord('a'))] = find(ord(eq[3]) - ord('a'))
88+
89+
for eq in equations:
90+
if eq[1] == '!' and find(ord(eq[0]) - ord('a')) == find(ord(eq[3]) - ord('a')):
91+
return False
92+
93+
return True
94+
```
95+
#### Java
96+
```java
97+
class Solution {
98+
public boolean equationsPossible(String[] equations) {
99+
int[] parent = new int[26];
100+
for (int i = 0; i < 26; ++i) {
101+
parent[i] = i;
102+
}
103+
104+
for (String eq : equations) {
105+
if (eq.charAt(1) == '=') {
106+
parent[find(parent, eq.charAt(0) - 'a')] = find(parent, eq.charAt(3) - 'a');
107+
}
108+
}
109+
110+
for (String eq : equations) {
111+
if (eq.charAt(1) == '!' && find(parent, eq.charAt(0) - 'a') == find(parent, eq.charAt(3) - 'a')) {
112+
return false;
113+
}
114+
}
115+
116+
return true;
117+
}
118+
119+
private int find(int[] parent, int x) {
120+
if (parent[x] != x) {
121+
parent[x] = find(parent, parent[x]);
122+
}
123+
return parent[x];
124+
}
125+
}
126+
```
127+
128+
return true;
129+
}
130+
};

0 commit comments

Comments
 (0)