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
sentence2 each represented as a string array and given an array of string pairs
similarPairs[i] = [xi, yi] indicates that the two words
yi are similar.
sentence2 are similar, or
false if they are not similar.
Two sentences are similar if:
- They have the same length (i.e. the same number of words)
Notice that a word is always similar to itself, also notice that the similarity relation is not transitive. For example, if the words
b are similar and the words
c are similar,
c are not necessarily similar.
Input: sentence1 = ["great","acting","skills"], sentence2 = ["fine","drama","talent"], similarPairs = [["great","fine"],["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.
Input: sentence1 = ["great"], sentence2 = ["great"], similarPairs =  Output: true Explanation: A word is similar to itself.
Input: sentence1 = ["great"], sentence2 = ["doubleplus","good"], similarPairs = [["great","doubleplus"]] Output: false Explanation: As they don't have the same length, we return false.
1 <= sentence1.length, sentence2.length <= 1000
1 <= sentence1[i].length, sentence2[i].length <= 20
sentence2[i]consist of lower-case and upper-case English letters.
0 <= similarPairs.length <= 1000
similarPairs[i].length == 2
1 <= xi.length, yi.length <= 20
yiconsist of lower-case and upper-case English letters.
- All the pairs
First compare the length of two sentences. Then check if words in sequences are in pairs.
class Solution: def areSentencesSimilar(self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]) -> bool: if len(sentence1) != len(sentence2): return False pairs_dict = defaultdict(set) for pair in similarPairs: pairs_dict[pair].add(pair) pairs_dict[pair].add(pair) for word1, word2 in zip(sentence1, sentence2): if word1 != word2 and word2 not in pairs_dict[word1]: return False return True
- Time Complexity: O(N).
- Space Complexity: O(N).