0% completed
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.
Solution
To solve this problem, we can use the Union-Find data structure. The Union-Find structure helps manage the equivalence classes of variables, making it efficient to determine if two variables are in the same class or not. The overall approach is to first process all equality equations to group variables into sets. After that, we process all inequality equations to ensure no contradictions exist between the sets.
This approach is effective because it separates the problem into manageable parts. First, it handles the equality constraints, ensuring all connected components are identified. Then, it checks the inequality constraints, confirming that no two variables in the same set violate the constraints. The Union-Find structure is optimal for this, as it provides near-constant time operations for union and find, ensuring the algorithm runs efficiently even for large inputs.
Step-by-step Algorithm
-
Initialize the parent array:
- Create an array
parent
of size 26 (for each lowercase letter). - Initialize each element of the array such that
parent[i] = i
.
- Create an array
-
Define the find function:
- This function takes an integer
x
and returns the root ofx
. - Use path compression to make future queries faster: if
parent[x]
is notx
, setparent[x]
to the result offind(parent[x])
.
- This function takes an integer
-
Define the union function:
- This function takes two integers
x
andy
and unions their sets. - Find the roots of
x
andy
using the find function. - If the roots are different, set the root of
x
to be the root ofy
.
- This function takes two integers
-
Process equality equations:
- Iterate through each equation in the input array.
- For each equation of the form "a==b":
- Convert
a
andb
to their respective indices (usinga - 'a'
andb - 'a'
). - Union the indices of
a
andb
.
- Convert
-
Process inequality equations:
- Iterate through each equation in the input array.
- For each equation of the form "a!=b":
- Convert
a
andb
to their respective indices. - Use the find function to check if the indices of
a
andb
have the same root. - If they do, return
false
(indicating a contradiction).
- Convert
-
Return the result:
- If no contradictions are found after processing all equations, return
true
.
- If no contradictions are found after processing all equations, return
Algorithm Walkthrough
Let's consider the input questions = ["a==b", "b==c", "a==c", "d==b"]
.
-
Initialization:
parent = [0, 1, 2, ..., 25]
-
Equation "a==b":
- Indices:
a -> 0
,b -> 1
- Union:
find(0) -> 0
,find(1) -> 1
- After union:
parent[0] = 1
- Updated parent array:
[1, 1, 2, 3, ..., 25]
- Indices:
-
Equation "b==c":
- Indices:
b -> 1
,c -> 2
- Union:
find(1) -> 1
,find(2) -> 2
- After union:
parent[1] = 2
- Updated parent array:
[1, 2, 2, 3, ..., 25]
- Indices:
-
Equation "a==c":
- Indices:
a -> 0
,c -> 2
- Union:
find(0) -> 1 -> 2
,find(2) -> 2
- After union: no change needed
- Updated parent array:
[2, 2, 2, 3, ..., 25]
- Indices:
-
Equation "d==b":
- Indices:
d -> 3
,b -> 1
- Union:
find(3) -> 3
,find(1) -> 2
- After union:
parent[3] = 2
- Updated parent array:
[2, 2, 2, 2, ..., 25]
- Indices:
-
Final check:
- No inequality equations to check.
- Return
true
.
Code
Complexity Analysis
Time Complexity
- The Union-Find operations (union and find) are very efficient, nearly O(1) due to path compression and union by rank.
- Processing each equation involves either a union or a find operation. Thus, for
n
equations, the time complexity is O(n).
Space Complexity
- We use an array to keep track of the parent of each variable, which has a fixed size of 26 (for the 26 lowercase letters). Thus, the space complexity is O(1).
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible