Sentence Similarity - Problem
Sentence Similarity is a fascinating problem that explores word relationships and pattern matching. Given two sentences represented as arrays of words and a list of word pairs that are considered similar, determine if the two sentences are equivalent.
The challenge is to check if two sentences are similar based on the following rules:
• Both sentences must have the same length
• Each word at position
• A word is always similar to itself
• Similarity is not transitive - if A~B and B~C, it doesn't mean A~C
For example, if we have sentences
The challenge is to check if two sentences are similar based on the following rules:
• Both sentences must have the same length
• Each word at position
i in sentence1 must be similar to the word at position i in sentence2• A word is always similar to itself
• Similarity is not transitive - if A~B and B~C, it doesn't mean A~C
For example, if we have sentences
["I", "love", "coding"] and ["I", "enjoy", "programming"] with similar pairs [["love", "enjoy"], ["coding", "programming"]], the sentences would be similar because each corresponding position has similar words. Input & Output
example_1.py — Basic Similarity
$
Input:
sentence1 = ["great", "acting", "skills"]
sentence2 = ["fine", "drama", "talent"]
similarPairs = [["great","fine"],["acting","drama"],["skills","talent"]]
›
Output:
true
💡 Note:
Each word at corresponding positions has a similarity pair: great~fine, acting~drama, and skills~talent. All positions match, so the sentences are similar.
example_2.py — Different Lengths
$
Input:
sentence1 = ["great"]
sentence2 = ["great", "good"]
similarPairs = []
›
Output:
false
💡 Note:
The sentences have different lengths (1 vs 2 words), so they cannot be similar regardless of the similarity pairs.
example_3.py — No Similarity Found
$
Input:
sentence1 = ["awesome", "acting", "skills"]
sentence2 = ["fine", "drama", "talent"]
similarPairs = [["great","fine"],["acting","drama"],["skills","talent"]]
›
Output:
false
💡 Note:
The first words 'awesome' and 'fine' are not similar (no pair exists), so the sentences are not similar even though the other positions would match.
Visualization
Tap to expand
Understanding the Visualization
1
Check Dictionary Size
Both sentences must have the same number of words to be translatable
2
Build Translation Map
Create a fast lookup table from all word pair translations (both directions)
3
Position-by-Position Check
For each word position, check if words are identical or translatable
4
Instant Lookup
Use hash set for O(1) translation checking instead of scanning entire dictionary
Key Takeaway
🎯 Key Insight: Converting similarity pairs to a hash set transforms O(m) searches into O(1) lookups, dramatically improving efficiency from O(n×m) to O(n+m)
Time & Space Complexity
Time Complexity
O(n + m)
n is sentence length, m is number of similar pairs. We iterate through sentences once (n) and build hash set once (m)
✓ Linear Growth
Space Complexity
O(m)
Hash set stores all similarity pairs, which is proportional to number of pairs (m)
✓ 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 ≤ xi.length, yi.length ≤ 20
- xi and yi consist of lowercase English letters
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code