题目链接:https://leetcode.cn/problems/count-prefix-and-suffix-pairs-i/description/
TypeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| function countPrefixSuffixPairs(words: string[]): number { let res = 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])) { res ++; } } } return res; }; function isPrefixAndSuffix(str1, str2): boolean { let i = 0, j = 0; while(i < str1.length) { if (str1[i] != str2[j]) { return false; } i++; j++; } i = 0, j = str2.length - str1.length; while(i < str1.length) { if (str1[i] != str2[j]) { return false; } i++; j++; } return true; }
|