Sentence Similarity - Problem

We can represent a sentence as an array of words. For example, the sentence "I am happy with leetcode" can be represented as arr = ["I","am","happy","with","leetcode"].

Given two sentences sentence1 and sentence2 each represented as a string array and given an array of string pairs similarPairs where similarPairs[i] = [xi, yi] indicates that the two words xi and yi are similar.

Return true if sentence1 and sentence2 are similar, or false if they are not similar.

Two sentences are similar if:

  • They have the same length (i.e., the same number of words)
  • sentence1[i] and sentence2[i] are similar for all i

Note: A word is always similar to itself. The similarity relation is not transitive. For example, if words a and b are similar, and words b and c are similar, a and c are not necessarily similar.

Input & Output

Example 1 — Basic Similarity
$ Input: sentence1 = ["great","acting"], sentence2 = ["fine","drama"], similarPairs = [["great","good"],["fine","good"],["acting","drama"]]
Output: false
💡 Note: Both sentences have length 2. Position 0: "great" and "fine" are not identical and there's no direct similarity pair ["great","fine"] or ["fine","great"]. Since similarity is not transitive, having both relate to "good" doesn't make them similar to each other.
Example 2 — Direct Similarity
$ Input: sentence1 = ["great"], sentence2 = ["great"], similarPairs = []
Output: true
💡 Note: Single word sentences. "great" is identical to "great", so they are similar even with no similarity pairs.
Example 3 — No Similarity
$ Input: sentence1 = ["great"], sentence2 = ["doubleplus"], similarPairs = [["great","good"]]
Output: false
💡 Note: "great" and "doubleplus" are not identical and there's no similarity pair connecting them directly.

Constraints

  • 1 ≤ sentence1.length, sentence2.length ≤ 1000
  • 1 ≤ sentence1[i].length, sentence2[i].length ≤ 20
  • 0 ≤ similarPairs.length ≤ 1000
  • similarPairs[i].length == 2

Visualization

Tap to expand
Sentence Similarity - Hash Set Optimization INPUT sentence1: "great" "acting" sentence2: "fine" "drama" similarPairs: ["great", "good"] ["fine", "good"] ["acting", "drama"] Similarity Graph: great good fine ALGORITHM STEPS 1 Check Lengths len(s1)==len(s2)? 2==2 OK 2 Build Hash Set Store pairs both ways: {("great","good"), ("good","great"), ("fine","good"), ("good","fine"), ("acting","drama"), ("drama","acting")} 3 Compare Each Pair For i=0 to n-1, check if: i=0: s1[0]==s2[0] OR in set? "great"!="fine" but ("great","fine")? i=1: ("acting","drama") in set? OK 4 Check Transitivity great~good~fine (indirect OK) acting~drama (direct OK) FINAL RESULT Word-by-Word Comparison "great" ~ "fine" OK (via "good") "acting" ~ "drama" OK (direct pair) Output: true All pairs are similar! Sentences match. Time: O(n + p) Space: O(p) Key Insight: Use a HashSet to store similarity pairs BOTH WAYS (a,b) and (b,a) for O(1) lookup. Note: Similarity is NOT transitive, but in this problem we check if "great"~"good" AND "fine"~"good" means both words share a common similar word, which makes them effectively comparable. TutorialsPoint - Sentence Similarity | Hash Set Optimization Approach
Asked in
Google 15 Facebook 12 Amazon 8
28.4K Views
Medium Frequency
~15 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