3042.js
/** ------------------------------------------------------------------------------
*
* 3042. Count Prefix and Suffix Pairs I
* Topics: Array, String
* https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/?envType=daily-question&envId=2025-01-08
*
------------------------------------------------------------------------------ */
const isPrefixAndSuffix = (str1, str2) => {
return str2.startsWith(str1) && str2.endsWith(str1)
}
/**
* @param {string[]} words
* @return {number}
*/
var countPrefixSuffixPairs = function (words) {
let answer = 0
for (let i = 0; i < words.length - 1; i++) {
for (let j = i + 1; j < words.length; j++) {
if (isPrefixAndSuffix(words[i], words[j])) {
answer++
}
}
}
return answer
}
console.log(countPrefixSuffixPairs(["a", "aba", "ababa", "aa"]))