3042-count-prefix-and-suffix-pairs-i

DevGod
Vtuber
/** * @param {string[]} words * @return {number} */var countPrefixSuffixPairs = function(words) { let ans = 0;
function isPrefixAndSuffix(str1, str2){ if( str1.length <= str2.length){ let prefix = str2.substring(0,str1.length); let suffix = str2.substring(str2.length-str1.length,str2.length); if( prefix === str1 && suffix === str1){ return true; } } return false; }
for(let I = 0; I<words.length; I++){ for(let A = I+1; A<words.length; A++){ if(isPrefixAndSuffix(words[I],words[A])){ ans++; } } }
return ans;};
function countPrefixSuffixPairs(words: string[]): number {
function isPrefixAndSuffix(str1:string, str2:string): boolean{ if(str1.length > str2.length){ return false; }
let prefix = str2.slice(0,str1.length); let suffix = str2.slice(-str1.length);
return (prefix == str1 && suffix == str1) }
let score:number = 0; for(let A = 0; A<words.length; A++){ for(let B = A+1; B<words.length; B++){ if(isPrefixAndSuffix(words[A],words[B])){ score++; } } }
return score;};