🔗 https://leetcode.com/problems/count-prefix-and-suffix-pairs-i
题目
- 一个字符串数组,返回其中有几对,word i 既是 word j 的前缀,也是后缀
思路
- 模拟,前缀匹配相同的 index,后缀匹配的 index 为 n2 - n1 + i
代码
class Solution {
public:bool check(string& s1, string& s2) {if (s1.size() > s2.size()) return false;bool mark = true;int n1 = s1.size();int n2 = s2.size();for (int i = 0; i < s1.size(); i++) {if (s1[i] != s2[i]) {mark = false;break;}if (s1[i] != s2[n2 - n1 + i]) {mark = false;break;}}return mark;}int countPrefixSuffixPairs(vector<string>& words) {int count = 0;for (int i = 0; i < words.size(); i++) {for (int j = i + 1; j < words.size(); j++) {if (check(words[i], words[j])) count++;}}return count;}
};