Easy
Sentence Similarity — Python
Full explanation · Time O(n + p) · Space O(p)
# Time: O(n + p)
# Space: O(p)
import itertools
class Solution(object):
def areSentencesSimilar(self, words1, words2, pairs):
"""
:type words1: List[str]
:type words2: List[str]
:type pairs: List[List[str]]
:rtype: bool
"""
if len(words1) != len(words2): return False
lookup = set(map(tuple, pairs))
return all(w1 == w2 or (w1, w2) in lookup or (w2, w1) in lookup \
for w1, w2 in itertools.izip(words1, words2))