行业知识
Leetcode3042. 统计前后缀下标对 I
2026-10-30 04:02:17
来源:互联网转载
Every day a Leetcode
题���来源:3042. 统计前后缀下标对 I
解法1:3042. 统计前后缀下标对 I
暴力枚举下标为 i 和 j 的字符串 words[i] 和 words[j],当满足条件:
words[i] == words[j].substr(0, words[i].size()) && words[i] == words[j].substr(words[j].size() - words[i].size()) 时,
计数器 count++,最后返回 count。
代码:
/* * @lc app=leetcode.cn id=3042 lang=cpp * * [3042] 统计前后缀下标对 I */// @lc code=startclass Solution{public: int countPrefixSuffixPairs(vector &words) { if (words.empty()) return 0; int n = words.size(), count = 0; for (int i = 0; i