-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges-62.js
42 lines (29 loc) · 1.06 KB
/
challenges-62.js
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
36
37
38
39
40
41
42
// #Question 2185. Counting Words With a Given Prefix
// You are given an array of strings words and a string pref.
// Return the number of strings in words that contain pref as a prefix.
// A prefix of a string s is any leading contiguous substring of s.
// Example 1:
// Input: words = ["pay","attention","practice","attend"], pref = "at"
// Output: 2
// Explanation: The 2 strings that contain "at" as a prefix are: "attention" and "attend".
// Example 2:
// Input: words = ["leetcode","win","loops","success"], pref = "code"
// Output: 0
// Explanation: There are no strings that contain "code" as a prefix.
// #Solution
var prefixCount = function (words, pref) {
let count = 0;
let a = words.map((item) => {
if (item.startsWith(pref)) {
count++;
}
});
return count;
};
prefixCount(["pay", "attention", "practice", "attend"], "at");
//or
var prefixCount = function (words, pref) {
const matchingWords = words.filter((word) => word.startsWith(pref));
return matchingWords.length;
};
prefixCount(["pay", "attention", "practice", "attend"], "at");