0 ms | Beats 100% | Easy to Understand | Counting Words With a Given Prefix
IntuitionWhen solving the problem, the key idea is to identify words in the array that start with the given prefix. A prefix means the beginning part of a word
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
When solving the problem, the key idea is to identify words in the array that start with the given prefix. A prefix means the beginning part of a word must match exactly with the given string, and we only need to check the first characters up to the length of the prefix for each word. The problem is straightforward if we focus on directly comparing the prefix with the initial part of each word.
# Approach
<!-- Describe your approach to solving the problem. -->
1. Iterate Through the Array:
   - Loop through each word in the words array.

2. Check Prefix Match:
   - For each word, compare its first len characters (where len is the length of pref) with the prefix string pref. This can be done using a substring comparison function like strncmp in C.

3. Count Matches:
   - If the prefix matches, increment a counter.

4. Return the Count:
   - After traversing all words in the array, return the count of matches.
  
5. Return the Count
# Complexity
- Time complexity: O(n⋅m), where:
  - n is the number of words in the words array.
  - m is the average length of words and the length of the prefix.
  - This is because comparing the prefix with the initial part of each word takes O(m) time for each of the n words.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1) (constant space).
  - No extra data structures are used.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```c []
int prefixCount(char** words, int wordsSize, char* pref) {
    int len = strlen(pref);
    int count = 0;
    for(int i = 0; i < wordsSize; i++){
        if(strncmp(words[i], pref, len) == 0){
            count ++;
        }
    }
    return count;
}
```
['Array', 'String', 'C', 'String Matching']