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 i in sentence1 is similar to the word at position i in sentence2

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 [("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
Word Similarity CommunitiesPositive Words CommunitygreatfinegoodniceAction Words CommunityactingdramaperformshowSentence Comparison ProcessSentence 1: ["great", "acting"]Sentence 2: ["good", "drama"]βœ“ Position 0: great ∈ Community 1, good ∈ Community 1βœ“ Position 1: acting ∈ Community 2, drama ∈ Community 2Result: Sentences are SIMILAR! πŸŽ‰All corresponding positions belong to same communities
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

n
2n
βœ“ Linear Growth
Space Complexity
O(W)

Union-Find structure storing parent and rank for each unique word W

n
2n
βœ“ 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
Asked in
Google 42 Facebook 38 Amazon 31 Microsoft 25
28.6K Views
Medium-High Frequency
~22 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen