Back to course home
0% completed
Vote For New Content
Satisfiability of Equality Equation (medium)
Problem Statement
You are given an array of strings called equations
. Each string in this array represents a relationship between two variables and follows one of two formats: "a==b" or "a!=b". Here, a
and b
are lowercase letters.
Return true
if it is possible to assign values to these variables such that all the given relationships are satisfied. Otherwise, return false
.
Examples
Example 1:
- Input: equations =
["a==b", "b==c", "a==c", "d == b"]
- Output:
true
- Explanation: All variables can be assigned the same value, satisfying all equations.
Example 2:
- Input: equations =
["a==b", "b!=c", "c==a"]
- Output:
false
- Explanation: If
a == b
andc == a
, thenb == c
, which contradictsb != c
.
Example 3:
- Input: equations =
["c==b", "b!=c"]
- Output:
false
- Explanation: The equations
c == b
andb != c
directly contradict each other.
Constraints:
- 1 <= equations.length <= 500
- equations[i].length == 4
- equations[i][0] is a lowercase letter.
- equations[i][1] is either '=' or '!'.
- equations[i][2] is '='.
- equations[i][3] is a lowercase letter.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
On this page
Problem Statement
Examples
Try it yourself