-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges-67.js
38 lines (28 loc) · 1.08 KB
/
challenges-67.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
// #Question 2255. Count Prefixes of a Given String
// You are given a string array words and a string s, where words[i] and s comprise only of lowercase English letters.
// Return the number of strings in words that are a prefix of s.
// A prefix of a string is a substring that occurs at the beginning of the string. A substring is a contiguous sequence of characters within a string.
// Example 1:
// Input: words = ["a","b","c","ab","bc","abc"], s = "abc"
// Output: 3
// Explanation:
// The strings in words which are a prefix of s = "abc" are:
// "a", "ab", and "abc".
// Thus the number of strings in words which are a prefix of s is 3.
// Example 2:
// Input: words = ["a","a"], s = "aa"
// Output: 2
// Explanation:
// Both of the strings are a prefix of s.
// Note that the same string can occur multiple times in words, and it should be counted each time.
// #Solution
var countPrefixes = function (words, s) {
let count = 0;
for (let word of words) {
if (s.startsWith(word)) {
count++;
}
}
return count;
};
countPrefixes(["a", "b", "c", "ab", "bc", "abc"], "abc");