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] = [xi, yi] indicates that the two words xi and yi are similar.
Return true if sentence1 and sentence2 are similar, or false if they are not similar.
Two sentences are similar if:
sentence1[i] and sentence2[i] are similar.Notice that a word is always similar to itself, also notice that the similarity relation is transitive. For example, if the words a and b are similar, and the words b and c are similar, then a and c are similar.
Example 1:
Input: sentence1 = ["great","acting","skills"], sentence2 = ["fine","drama","talent"], similarPairs = [["great","good"],["fine","good"],["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 = ["I","love","leetcode"], sentence2 = ["I","love","onepiece"], similarPairs = [["manga","onepiece"],["platform","anime"],["leetcode","platform"],["anime","manga"]] Output: true Explanation: "leetcode" --> "platform" --> "anime" --> "manga" --> "onepiece". Since "leetcode is similar to "onepiece" and the first two words are the same, the two sentences are similar.
Example 3:
Input: sentence1 = ["I","love","leetcode"], sentence2 = ["I","love","onepiece"], similarPairs = [["manga","hunterXhunter"],["platform","anime"],["leetcode","platform"],["anime","manga"]] Output: false Explanation: "leetcode" is not similar to "onepiece".
Constraints:
1 <= sentence1.length, sentence2.length <= 10001 <= sentence1[i].length, sentence2[i].length <= 20sentence1[i] and sentence2[i] consist of lower-case and upper-case English letters.0 <= similarPairs.length <= 2000similarPairs[i].length == 21 <= xi.length, yi.length <= 20xi and yi consist of English letters.Problem Overview: You are given two sentences and a list of similar word pairs. Words are considered similar if they are directly or indirectly connected through these pairs. The task is to determine whether the two sentences are similar by checking if each corresponding word belongs to the same similarity group.
Approach 1: Graph Traversal with DFS/BFS (O(n + p) time, O(p) space)
Treat each word as a node in a graph and every similar pair as an undirected edge. Build an adjacency list using a hash table where each word maps to its neighbors. For every position i in the sentences, if the words differ, run a DFS or BFS from the first word to see if the second word is reachable. The key insight is that similarity is transitive, so reachability in the graph determines equivalence. This works well when the number of similarity pairs is moderate and you want an explicit traversal of relationships.
Approach 2: Union Find / Disjoint Set (O(n + p α(p)) time, O(p) space)
Model the problem as connected components using Union Find. Each unique word belongs to a disjoint set. Iterate through the similarity pairs and perform union(a, b) so both words share the same root. After building the structure, compare the sentences word by word. If two words are identical, continue. Otherwise check whether find(word1) == find(word2). Path compression and union by rank keep operations nearly constant time. This approach avoids repeated graph traversals and scales better when the pair list is large.
Recommended for interviews: Union Find is typically the expected solution. It directly models the transitive similarity relationship and provides near O(1) connectivity checks after preprocessing. Implementing the DFS/BFS graph approach first demonstrates understanding of the problem as a connectivity search, while switching to Union Find shows stronger algorithmic design and optimization skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Graph Traversal (DFS/BFS) | O(n + p) | O(p) | When you want an explicit graph representation and occasional reachability checks |
| Union Find (Disjoint Set) | O(n + p α(p)) | O(p) | Best general solution for large synonym sets and repeated similarity queries |
花花酱 LeetCode 737. Sentence Similarity II- 刷题找工作 EP118 • Hua Hua • 4,360 views views
Watch 6 more video solutions →Practice Sentence Similarity II with our built-in code editor and test cases.
Practice on FleetCode