Sentence Similarity II - Problem
Sentence Similarity with Transitive Relations
Given two sentences represented as string arrays and a list of word similarity pairs, determine if the sentences are similar.
Two sentences are considered similar if:
β’ They have the same length (same number of words)
β’ Each word at position
The key challenge is that similarity is transitive: if word A is similar to word B, and word B is similar to word C, then A and C are also similar. Every word is always similar to itself.
Example: If we have pairs
Your task is to efficiently determine if two sentences can be considered similar given these transitive similarity relationships.
Given two sentences represented as string arrays and a list of word similarity pairs, determine if the sentences are similar.
Two sentences are considered similar if:
β’ They have the same length (same number of words)
β’ Each word at position
i in sentence1 is similar to the word at position i in sentence2The key challenge is that similarity is transitive: if word A is similar to word B, and word B is similar to word C, then A and C are also similar. Every word is always similar to itself.
Example: If we have pairs
[("great", "fine"), ("fine", "good")], then "great" and "good" are similar through the transitive relationship.Your task is to efficiently determine if two sentences can be considered similar given these transitive similarity relationships.
Input & Output
example_1.py β Basic Similarity
$
Input:
sentence1 = ["great", "acting", "skills"]
sentence2 = ["fine", "drama", "talent"]
similarPairs = [["great","fine"],["drama","acting"],["skills","talent"]]
βΊ
Output:
true
π‘ Note:
Each word at corresponding positions are directly similar: 'great'β'fine', 'acting'β'drama', 'skills'β'talent'. Since all positions match through direct similarity pairs, the sentences are similar.
example_2.py β Transitive Similarity
$
Input:
sentence1 = ["great", "acting"]
sentence2 = ["good", "drama"]
similarPairs = [["great","fine"],["fine","good"],["acting","drama"]]
βΊ
Output:
true
π‘ Note:
Position 0: 'great' and 'good' are similar through transitive relationship: greatβfineβgood. Position 1: 'acting' and 'drama' are directly similar. Both positions satisfy similarity requirements.
example_3.py β Different Lengths
$
Input:
sentence1 = ["I", "love", "coding"]
sentence2 = ["I", "love"]
similarPairs = []
βΊ
Output:
false
π‘ Note:
The sentences have different lengths (3 vs 2 words), so they cannot be similar regardless of similarity pairs. This is caught immediately without processing any pairs.
Visualization
Tap to expand
Understanding the Visualization
1
Build Social Circles
Start with everyone as individual nodes, then merge friends into communities
2
Process Friendships
For each friendship pair, union their communities using efficient path compression
3
Query Relationships
To check if two people are connected, verify they belong to the same community
Key Takeaway
π― Key Insight: Union-Find transforms the complex problem of checking transitive relationships into simple community membership queries, achieving near-constant time complexity for each comparison.
Time & Space Complexity
Time Complexity
O(P Γ Ξ±(W) + N Γ Ξ±(W))
P union operations for similarity pairs plus N find operations for sentence comparison, where Ξ± is inverse Ackermann
β Linear Growth
Space Complexity
O(W)
Union-Find structure storing parent and rank for each unique word W
β Linear Space
Constraints
- 1 β€ sentence1.length, sentence2.length β€ 1000
- 1 β€ sentence1[i].length, sentence2[i].length β€ 20
- sentence1[i] and sentence2[i] consist of lowercase English letters
- 0 β€ similarPairs.length β€ 2000
- similarPairs[i].length == 2
- 1 β€ similarPairs[i][0].length, similarPairs[i][1].length β€ 20
- similarPairs[i][0] and similarPairs[i][1] consist of lowercase English letters
- Key insight: Similarity relation is transitive and reflexive
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code