Sentence Similarity
Description
null
Solution(javascript)
/**
* @param {string[]} words1
* @param {string[]} words2
* @param {string[][]} pairs
* @return {boolean}
*/
var areSentencesSimilar = function(words1, words2, pairs) {
if(words1.length !== words2.length) {
return false
}
const map = pairs.reduce((acc, [a, b]) =>{
acc[a] = acc[a] || []
acc[a].push(b)
acc[b] = acc[b] || []
acc[b].push(a)
return acc
},{})
return words1.every((word1, index) => {
const word2 = words2[index]
return word1 === word2 || (map[word1] || []).indexOf(word2) > -1
})
};