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.
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
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code