Equation satisfiability check
Last Updated :
18 Oct, 2023
Given an array arr[] of size N where each arr[i] is a string denoting an equation, the task is to return true if all the statements are satisfactory otherwise return false if any two equations contradict each other. An equation can be one of the two types below:
- Type 1: “a != b”, which denotes that a is not equal to b
- Type 2: “a == b”, which denotes that both a and b are equal
Note: The provided equations are transitive in nature (i.e. if “a==b” and “b==c“, then “a==c“).
Examples:
Input: N = 3, arr[] = [“a==b”, “a==c”, “b != c”]
Output: False
Explanation: Given “a == b” and “a == c”, using the transitive property we get “b == c “, but arr[2] i.e. “b != c” contradicts that, so we print False.
Input: N = 2, arr = [“a == b”, “b == a”]
Output: True
Explanation: Clearly arr[0] and arr[1] are same equations and there does not exists any contradicting equation, hence we print True.
Efficient Approach: Using Disjoint Set Union (DSU) to solve the equations efficiently.
Since the Equations follow the transitive property, we can use Union operation of DSU to put together all the equal equations in a single set, for example if we have {a==b, b==c, d==e} then we will have two sets i.e. {a,b,c} and {d,e} . We can process all the equations of type 2 first using DSU and finally check all the equation of type 1 and retrieve our answer as per the below two cases:
- Case 1: if both the variables of type 1 equation belong to same set
- Case 2: if all the type1 equation does not follow Case1
Follow the steps to solve the problem:
- Firstly, initialize the DSU data structure, i.e. Find(), Union(), and parent[] array.
- Make the parent of each character itself.
- For each equation of type 2, merge the variables using the Union operation
- Now for each equation of type 1, if the variables belong to the same disjoint set then return “False” else return “True“.
Below is the implementation of the above algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
int parent[26];
int find( int v)
{
if (v == parent[v])
return v;
return parent[v] = find(parent[v]);
}
void Union( int a, int b)
{
a = find(a);
b = find(b);
if (a != b) {
parent[b] = a;
}
}
string satisfactoryEquation(vector<string>& arr)
{
for ( int i = 0; i < 26; i++)
parent[i] = i;
for ( auto e : arr) {
int a = e[0] - 'a' ;
int b = e[3] - 'a' ;
char c = e[1];
if (c == '=' ) {
Union(a, b);
}
}
for ( auto e : arr) {
if (e[1] == '!' ) {
int a = e[0] - 'a' ;
int b = e[3] - 'a' ;
if (find(a) == find(b))
return "False" ;
}
}
return "True" ;
}
int main()
{
int N = 3;
vector<string> arr = { "a==b" , "a==c" , "b!=c" };
cout << satisfactoryEquation(arr);
}
|
Java
import java.util.*;
public class Main {
static int [] parent = new int [ 26 ];
static int find( int v)
{
if (v == parent[v])
return v;
return parent[v] = find(parent[v]);
}
static void union( int a, int b)
{
a = find(a);
b = find(b);
if (a != b) {
parent[b] = a;
}
}
static String
satisfactoryEquation(ArrayList<String> arr)
{
for ( int i = 0 ; i < 26 ; i++)
parent[i] = i;
for (String e : arr) {
int a = e.charAt( 0 ) - 'a' ;
int b = e.charAt( 3 ) - 'a' ;
char c = e.charAt( 1 );
if (c == '=' ) {
union(a, b);
}
}
for (String e : arr) {
if (e.charAt( 1 ) == '!' ) {
int a = e.charAt( 0 ) - 'a' ;
int b = e.charAt( 3 ) - 'a' ;
if (find(a) == find(b))
return "False" ;
}
}
return "True" ;
}
public static void main(String[] args)
{
int N = 3 ;
ArrayList<String> arr = new ArrayList<String>(
Arrays.asList( "a==b" , "a==c" , "b!=c" ));
System.out.println(satisfactoryEquation(arr));
}
}
|
Python3
parent = list ( range ( 26 ))
def find(v):
if v = = parent[v]:
return v
parent[v] = find(parent[v])
return parent[v]
def Union(a, b):
a = find(a)
b = find(b)
if a ! = b:
parent[b] = a
def satisfactoryEquation(arr):
for i in range ( 26 ):
parent[i] = i
for e in arr:
a = ord (e[ 0 ]) - ord ( 'a' )
b = ord (e[ 3 ]) - ord ( 'a' )
c = e[ 1 ]
if c = = '=' :
Union(a, b)
for e in arr:
if e[ 1 ] = = '!' :
a = ord (e[ 0 ]) - ord ( 'a' )
b = ord (e[ 3 ]) - ord ( 'a' )
if find(a) = = find(b):
return "False"
return "True"
if __name__ = = "__main__" :
N = 3
arr = [ "a==b" , "a==c" , "b!=c" ]
print (satisfactoryEquation(arr))
|
C#
using System;
public class GFG
{
static int [] parent = new int [26];
static int Find( int v)
{
if (v == parent[v])
return v;
return parent[v] = Find(parent[v]);
}
static void Union( int a, int b)
{
a = Find(a);
b = Find(b);
if (a != b)
{
parent[b] = a;
}
}
static string SatisfactoryEquation( string [] arr)
{
for ( int i = 0; i < 26; i++)
{
parent[i] = i;
}
foreach ( string e in arr)
{
int a = e[0] - 'a' ;
int b = e[3] - 'a' ;
char c = e[1];
if (c == '=' )
{
Union(a, b);
}
}
foreach ( string e in arr)
{
if (e[1] == '!' )
{
int a = e[0] - 'a' ;
int b = e[3] - 'a' ;
if (Find(a) == Find(b))
{
return "False" ;
}
}
}
return "True" ;
}
public static void Main()
{
string [] arr = { "a==b" , "a==c" , "b!=c" };
Console.WriteLine(SatisfactoryEquation(arr));
}
}
|
Javascript
const parent = {};
function GFG(v) {
if (v === parent[v]) {
return v;
}
return parent[v] = GFG(parent[v]);
}
function union(a, b) {
a = GFG(a);
b = GFG(b);
if (a !== b) {
parent[b] = a;
}
}
function satisfactoryEquation(arr) {
for (let i = 0; i < 26; i++) {
parent[String.fromCharCode( 'a' .charCodeAt(0) + i)] = String.fromCharCode( 'a' .charCodeAt(0) + i);
}
for (const e of arr) {
const a = e.charCodeAt(0) - 'a' .charCodeAt(0);
const b = e.charCodeAt(3) - 'a' .charCodeAt(0);
const c = e[1];
if (c === '=' ) {
union(String.fromCharCode( 'a' .charCodeAt(0) + a), String.fromCharCode( 'a' .charCodeAt(0) + b));
}
}
for (const e of arr) {
if (e[1] === '!' ) {
const a = e.charCodeAt(0) - 'a' .charCodeAt(0);
const b = e.charCodeAt(3) - 'a' .charCodeAt(0);
if (GFG(String.fromCharCode( 'a' .charCodeAt(0) + a)) === GFG(String.fromCharCode( 'a' .charCodeAt(0) + b))) {
return "False" ;
}
}
}
return "True" ;
}
function main() {
const arr = [ "a==b" , "a==c" , "b!=c" ];
console.log(satisfactoryEquation(arr));
}
main();
|
Time Complexity: O(N), where N is the number of equations
Auxiliary Space: O(26), for the parent array
Share your thoughts in the comments
Please Login to comment...