734 - Sentence Similarity
Written on February 28, 2018
Tweet
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. Note that the similarity relation is not transitive. For example, if “great” and “fine” are similar, and “fine” and “good” are similar, “great” and “good” are not necessarily similar.
class Solution:
def areSentencesSimilar(self, words1: List[str], words2: List[str], pairs: List[List[str]]) -> bool:
if len(words1) != len(words2):
return False
word_map = collections.defaultdict(set)
for (a, b) in pairs:
word_map[a].add(b)
word_map[b].add(a)
for w1, w2 in zip(words1, words2):
if w1 != w2 and w2 not in word_map[w1]:
return False
return True