hardGraphsPure DSA~30 min
Are the Equality and Inequality Constraints Satisfiable
A config validator receives constraints like 'a == b' and 'c != d' over single-letter variables. Decide whether some assignment of values satisfies all of them simultaneously.
Problem
Given an array equations of strings each of length 4 in the form 'x==y' or 'x!=y' (x, y lowercase letters), return true if there exists an assignment of integers to variables that satisfies every equation, and false otherwise.
Input
An array equations of 4-character constraint strings.
Output
A boolean: whether all constraints can be satisfied.
Constraints
- 1 <= equations.length <= 500
- Each equation has the exact form a==b or a!=b.
- Variable names are single lowercase letters.
Examples
Example 1
Input
equations = ["a==b","b!=a"]
Output
false
a==b forces equality, but b!=a forbids it.
Example 2
Input
equations = ["b==a","a==b"]
Output
true
Both demand a and b be equal — consistent.