Counting Words with Prefixes

Easy
24
81.7% Acceptance

In this lab, you will be working on a function that can count the number of words that contain a given prefix. The objective is to implement a function named prefixCount that receives an array of strings words and a string pref, and returns the number of strings in words that have pref as a prefix. A prefix of a string s is any leading contiguous substring of s.

Examples

Consider the following examples to understand how the function should work:

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.

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length, pref.length <= 100
  • words[i] and pref consist of lowercase English letters.

Challenges

In this lab, you will complete the following challenges:

  1. Implement the prefixCount function with ESM import/export
  2. Export the prefixCount function
  3. Test the function with provided examples and additional test cases

For each challenge, the evaluation script will be testing your solution to ensure it meets the requirements. Make sure to export the necessary functions or variables in your code to be used by the evaluation script.