✅100% || C++ || Python || String Matching Algorithms ||🔥Efficient Prefix Count Solutions || Easy
IntuitionThe goal is to determine how many strings in the words list start with the given prefix pref. This can be efficiently done by checking the prefix of ea
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The goal is to determine how many strings in the words list start with the given prefix pref. This can be efficiently done by checking the prefix of each string in the list.
# Approach
<!-- Describe your approach to solving the problem. -->
We can iterate through the words list and check if each word starts with the prefix pref using different methods. If a word starts with the prefix, we increment our count. We can achieve this using several approaches:

1. Using the startswith method and slicing to compare substrings in Python

2. Using various C++ methods (substr(), find(), mismatch(), equal(), compare()).
# Complexity
- Time complexity: All solutions have a time complexity of $$O(n)$$, where $$n$$ is the number of strings in the words list, since we need to check each word.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: All solutions have a space complexity of $$O(1)$$, since we only use a few extra variables to store the count and do not use additional data structures.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code - C++
- using substr()
```cpp []
class Solution {
public:
    int prefixCount(vector<string>& words, string pref) {
        int c=0,p=pref.size();
        for(string w:words){
            if(w.substr(0,p)==pref)
                c++;
        }
        return c;
    }
};
```
- using find()
```cpp []
class Solution {
public:
    int prefixCount(vector<string>& words, string pref) {
        int c=0;
        for(string w:words){
            if (w.find(pref)==0) 
                c++;
        }
        return c;
    }
};
```
- using mismatch()
```cpp []
class Solution {
public:
    int prefixCount(vector<string>& words, string pref) {
        int c=0;
        for(string w:words){
            if(mismatch(pref.begin(), pref.end(), w.begin()).first == pref.end()) 
                c++;
        }
        return c;
    }
};
```
- using equal()
```cpp []
class Solution {
public:
    int prefixCount(vector<string>& words, string pref) {
        int c=0;
        for(string w:words){
            if (w.size() >= pref.size() && equal(pref.begin(), pref.end(), w.begin()))
                c++;
        }
        return c;
    }
};
```
- using compare()
```cpp []
class Solution {
public:
    int prefixCount(vector<string>& words, string pref) {
        int c=0;
        for(string w:words){
            if (w.compare(0, pref.size(), pref) == 0)
                c++;
        }
        return c;
    }
};
```
# Code - Python
- using startswith()
```python []
class Solution:
    def prefixCount(self, words: List[str], pref: str) -> int:
        return sum(w.startswith(pref) for w in words)
```
- using slicing
```python []
class Solution:
    def prefixCount(self, words: List[str], pref: str) -> int:
        c = 0 
        for w in words: 
            if w[:len(pref)] == pref: 
                c += 1 
        return c
```
['Array', 'String', 'String Matching', 'C++', 'Python3']