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.

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
Sentence Similarity II - Union-Find Approach INPUT sentence1: great acting skills sentence2: fine drama talent similarPairs (Graph): great fine acting drama skills talent ALGORITHM STEPS 1 Check Lengths Both sentences: 3 words 2 Build Union-Find Union similar words union("great", "fine") union("acting", "drama") union("skills", "talent") Creates 3 disjoint sets 3 Compare Words Check each position Pos Word1 Word2 Same? 0 great fine OK 1 acting drama OK 2 skills talent OK 4 Return Result All positions match! FINAL RESULT Union-Find Sets: Set 1 great -- fine Set 2 acting -- drama Set 3 skills -- talent Output: true All word pairs belong to the same sets! Key Insight: Union-Find (Disjoint Set Union) handles transitive similarity efficiently. Each connected component represents words that are similar to each other. Using path compression and union by rank, we achieve near O(1) amortized time for each find/union operation. Time: O(N * alpha(N)), Space: O(N) TutorialsPoint - Sentence Similarity II | Union-Find Approach
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