Sentence Similarity Problem
Description
LeetCode Problem 734.
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] = [x_i, y_i] indicates that the two words x_i and y_i 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]andsentence2[i]are similar.
Notice that a word is always similar to itself, also notice that the similarity relation is not transitive. For example, if the words a and b are similar, and the words b and c are similar, a and c are not necessarily similar.
Example 1:
Input: sentence1 = ["great","acting","skills"], sentence2 = ["fine","drama","talent"], similarPairs = [["great","fine"],["drama","acting"],["skills","talent"]]
Output: true
Explanation: The two sentences have the same length and each word i of sentence1 is also similar to the corresponding word in sentence2.
Example 2:
Input: sentence1 = ["great"], sentence2 = ["great"], similarPairs = []
Output: true
Explanation: A word is similar to itself.
Example 3:
Input: sentence1 = ["great"], sentence2 = ["doubleplus","good"], similarPairs = [["great","doubleplus"]]
Output: false
Explanation: As they don't have the same length, we return false.
Constraints:
1 <= sentence1.length, sentence2.length <= 10001 <= sentence1[i].length, sentence2[i].length <= 20sentence1[i]andsentence2[i]consist of English letters.0 <= similarPairs.length <= 1000similarPairs[i].length == 21 <= x_i.length, y_i.length <= 20x_iandy_iconsist of lower-case and upper-case English letters.- All the pairs
(x_i,y_i)are distinct.
Solution
Approach: Hash Set of Pairs
If the two sentences have different lengths, they cannot be similar.
Otherwise, put every similar pair (x, y) in a hash set. Then compare the sentences word by word: words a and b at the same position are fine if a == b, if (a, b) is in the set, or if (b, a) is. The relation works in both directions, but each pair is stored only one way, so we must look up both orders.
Two points to watch:
- Similarity is not transitive. With pairs
a ~ bandb ~ c, the wordsaandcare not similar, so a direct set lookup is exactly right. No union-find is needed (that is the follow-up problem, Sentence Similarity II). - Words are case-sensitive:
"Great"and"great"are different words unless a pair links them.
Complexity: O(L) time and space, where L is the total length of all the words and pairs.
class Solution:
def areSentencesSimilar(self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]) -> bool:
if len(sentence1) != len(sentence2):
return False
pairs = {(x, y) for x, y in similarPairs}
for a, b in zip(sentence1, sentence2):
# A pair works in both directions, and a word is always similar to itself.
if a != b and (a, b) not in pairs and (b, a) not in pairs:
return False
return True