class Solution:
def areSentencesSimilar(self, sentence1, sentence2, similarPairs):
if len(sentence1) != len(sentence2):
return False
similar = set()
for a, b in similarPairs:
similar.add((a, b))
similar.add((b, a))
for w1, w2 in zip(sentence1, sentence2):
if w1 != w2 and (w1, w2) not in similar:
return False
return True
class Solution {
public:
bool areSentencesSimilar(vector<string>& sentence1, vector<string>& sentence2, vector<vector<string>>& similarPairs) {
if (sentence1.size() != sentence2.size()) return false;
unordered_set<string> similar;
for (auto& pair : similarPairs) {
similar.insert(pair[0] + "#" + pair[1]);
similar.insert(pair[1] + "#" + pair[0]);
}
for (int i = 0; i < sentence1.size(); ++i) {
if (sentence1[i] != sentence2[i] && similar.find(sentence1[i] + "#" + sentence2[i]) == similar.end()) {
return false;
}
}
return true;
}
};
class Solution {
public boolean areSentencesSimilar(String[] sentence1, String[] sentence2, List<List<String>> similarPairs) {
if (sentence1.length != sentence2.length) return false;
Set<String> similar = new HashSet<>();
for (List<String> pair : similarPairs) {
similar.add(pair.get(0) + "#" + pair.get(1));
similar.add(pair.get(1) + "#" + pair.get(0));
}
for (int i = 0; i < sentence1.length; ++i) {
if (!sentence1[i].equals(sentence2[i]) && !similar.contains(sentence1[i] + "#" + sentence2[i])) {
return false;
}
}
return true;
}
}
var areSentencesSimilar = function(sentence1, sentence2, similarPairs) {
if (sentence1.length !== sentence2.length) return false;
const similar = new Set();
for (const [a, b] of similarPairs) {
similar.add(a + "#" + b);
similar.add(b + "#" + a);
}
for (let i = 0; i < sentence1.length; i++) {
if (sentence1[i] !== sentence2[i] && !similar.has(sentence1[i] + "#" + sentence2[i])) {
return false;
}
}
return true;
};
Given two sentences, each represented as a list of words (sentence1
and sentence2
), and a list of word pairs (similarPairs
), determine if the two sentences are similar. Two words are considered similar if they are the same word or if they appear together as a pair in similarPairs
. The similarity relation is symmetric but not transitive. The sentences are similar only if they have the same length and every corresponding pair of words is similar.
similarPairs
consists of two strings.(a, b)
is in similarPairs
, then (b, a)
is also similar.similarPairs
; each comparison is independent.
To solve this problem, the first step is to compare the lengths of the two sentences. If they are not the same, they cannot be similar. Next, for each corresponding pair of words, check if they are either identical or listed as similar in similarPairs
.
A brute-force solution would check every pair in similarPairs
for each word, which is inefficient. To optimize, we can store all similar pairs in a set for constant-time lookups. This way, we can quickly check if two words are similar without scanning the entire list each time.
The main idea is to use a data structure that allows for fast similarity checking, and to ensure we check every word pair only once.
sentence1
and sentence2
have the same length. If not, return False
immediately.similarPairs
in both orders (to account for symmetry).False
.True
.We use a set for similar pairs because it allows O(1) lookup time, making the solution efficient.
Consider the following example:
sentence1 = ["great", "acting", "skills"]
sentence2 = ["fine", "drama", "talent"]
similarPairs = [["great", "fine"], ["drama", "acting"], ["skills", "talent"]]
Let's walk through the process:
True
.similarPairs
(O(N * M), where N is the sentence length and M is the number of pairs).The solution efficiently checks sentence similarity by leveraging a set for fast lookups of similar word pairs. By ensuring both sentences are the same length and every word pair is either identical or marked as similar, we guarantee correctness. The use of a set for O(1) lookups makes the approach both simple and optimal for the problem's constraints.