Skip to content

Commit 520dc4d

Browse files
committed
Add solution #3042
1 parent a98fdda commit 520dc4d

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@
405405
2677|[Chunk Array](./2677-chunk-array.js)|Easy|
406406
2695|[Array Wrapper](./2695-array-wrapper.js)|Easy|
407407
2703|[Return Length of Arguments Passed](./2703-return-length-of-arguments-passed.js)|Easy|
408+
3042|[Count Prefix and Suffix Pairs I](./3042-count-prefix-and-suffix-pairs-i.js)|Easy|
408409
3110|[Score of a String](./3110-score-of-a-string.js)|Easy|
409410
3392|[Count Subarrays of Length Three With a Condition](./3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|
410411
3396|[Minimum Number of Operations to Make Elements in Array Distinct](./3396-minimum-number-of-operations-to-make-elements-in-array-distinct.js)|Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 3042. Count Prefix and Suffix Pairs I
3+
* https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/
4+
* Difficulty: Easy
5+
*
6+
* You are given a 0-indexed string array words.
7+
*
8+
* Let's define a boolean function isPrefixAndSuffix that takes two strings, str1 and str2:
9+
* - isPrefixAndSuffix(str1, str2) returns true if str1 is both a prefix and a suffix of
10+
* str2, and false otherwise.
11+
*
12+
* For example, isPrefixAndSuffix("aba", "ababa") is true because "aba" is a prefix of
13+
* "ababa" and also a suffix, but isPrefixAndSuffix("abc", "abcd") is false.
14+
*
15+
* Return an integer denoting the number of index pairs (i, j) such that i < j, and
16+
* isPrefixAndSuffix(words[i], words[j]) is true.
17+
*/
18+
19+
/**
20+
* @param {string[]} words
21+
* @return {number}
22+
*/
23+
var countPrefixSuffixPairs = function(words) {
24+
let count = 0;
25+
26+
for (let i = 0; i < words.length; i++) {
27+
for (let j = i + 1; j < words.length; j++) {
28+
if (words[j].startsWith(words[i]) && words[j].endsWith(words[i])) {
29+
count++;
30+
}
31+
}
32+
}
33+
34+
return count;
35+
};

0 commit comments

Comments
 (0)