Sentence Similarity II
Difficulty: Medium
Category: DSA
Topics: Array, Hash Table, String, Depth-First Search, Breadth-First Search, Union Find
Asked at: Amazon
You are given two string arrays `sentence1` and `sentence2`, each of length `n`, and an array of pairs `similarPairs` where each `similarPairs[i] = [xi, yi]` indicates that the words `xi` and `yi` are similar.
A word `a` is considered similar to word `b` if:
- `a` and `b` are the same word, or
- `a` and `b` are directly similar (i.e., `[a, b]` or `[b, a]` is in `similarPairs`), or
- `a` and `b` are transitively similar (i.e., there exists a sequence of words connecting `a` to `b` through direct similarities).
Note that similarity is **symmetric** and **transitive**.
Return _true_ if `sentence1` and `sentence2` are similar (i.e., for every index `i`, `sentence1[i]` is similar to `sentence2[i]`); otherwise, return _false_.
**Constraints:**
- `1 <= sentence1.length, sentence2.length <= 1000`
- `1 <= sentence1[i].length, sentence2[i].length <= 20`
- `0 <= similarPairs.length <= 2000`
- `similarPairs[i].length == 2`
- All words consist of lowercase English letters only.