Skip to content

Commit e7791ad

Browse files
authored
Create 3042. Count Prefix and Suffix Pairs I (#685)
2 parents 1bb94fd + ac5da96 commit e7791ad

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

3042. Count Prefix and Suffix Pairs I

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
bool isPrefix(string word1, string word2){
4+
if(word1.size() > word2.size()) return false;
5+
for(int i=0; i<word1.size(); i++){
6+
if(word1[i] != word2[i]) return false;
7+
}
8+
return true;
9+
}
10+
bool isSuffix(string word1, string word2){
11+
if(word1.size() > word2.size()) return false;
12+
int i=word1.size()-1, j=word2.size()-1;
13+
while(i>=0){
14+
if(word1[i] != word2[j]) return false;
15+
i--, j--;
16+
}
17+
return true;
18+
}
19+
int countPrefixSuffixPairs(vector<string>& words) {
20+
int count = 0;
21+
for(int i=0; i<words.size(); i++){
22+
for(int j=i+1; j<words.size(); j++){
23+
string word1=words[i], word2=words[j];
24+
if(isPrefix(word1, word2) && isSuffix(word1, word2)) count++;
25+
}
26+
}
27+
return count;
28+
}
29+
};

0 commit comments

Comments
 (0)