Trivial Python solution using set comprehension

  • 0
    def areSentencesSimilar(self, words1, words2, pairs):
        if len(words1) != len(words2): return False
        s = {tuple(p) for p in pairs}
        return all(
            (w1 == w2) or ((w1,w2) in s) or ((w2,w1) in s)
            for w1,w2 in zip(words1, words2))

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.