Beats 100.00% ✅✅ | Easy Code | Brute Force🔥
Intuition

There is not so much to observe, just brute force and check for every string present in words, whether it contains "pref" as prefix or not.


For exa
![Screenshot 2025-01-09 110411.png](https://assets.leetcode.com/users/images/e5fe5475-72ae-43a0-ba46-193f3f6f8db1_1736401508.4626627.png)

# Intuition
- There is not so much to observe, just brute force and check for every string present in words, whether it contains *"pref"* as prefix or not.

- For example a string from words is *"str"*. Assume **n = size of "str"**, **m = size of "pref"**, now if n < m, that means string *"str"* can never contain whole *"pref"* as a prefix.
- If n >= m, that means we only need to check whether the first m characters of *"str"* are same as *"pref"* or not, so for this we just brute force over the length of *"pref"*.

# Approach
1. Outer loop is for all the strings present in words.
2. If n < m so no need to check we just skip this string, otherwise if n >= m, just loop for prefix and check whether **str[i] == pref[i]** or not, if at any point they dont match, that means *"pref"* is not present as prefix, so we just break.
3. Otherwise if we reach the end , i.e **i == m-1** and still we are inside the loop that means all character till now are same in *"str"* and *"pref"*, so we increment our answer.
4. Finally return our answer.

# Complexity
- Time complexity:
1. We loop over all strings present in words, and for each string we again loop for size of *"pref"*, to check whether *"pref"* is present as prefix or not.
2. **T.C - O(words.size() * pref.size())**

- Space complexity:
1. No additional space has been used for solving the problem.
2. **S.C - O(1)**

# Code
```cpp []
class Solution {
public:
    int prefixCount(vector<string>& words, string pref) {
        int ans = 0;
        for(auto str : words){
            int n = str.size(),m = pref.size();
            if(n < m)continue;
            for(int i=0;i<m;i++){
                if(str[i] != pref[i]){
                    break;
                }
                if(i == m-1)ans++;
            }
        }
        return ans;
    }
};
```
['String', 'String Matching', 'C++']