🔥Beats 💯|| 2 methods 🎯|| C++ || Find || Substring || Easy 🚀✅
Intuition📚We check all strings in vector and if prefix of that string match pref string given -> increment count for the result.Approach🛠️Version 1: Find in a s
# Intuition📚

We check all strings in vector and if prefix of that string match pref string given -> increment count for the result.

---


# Approach🛠️
# Version 1: Find in a string
A loop for checking all strings in the array and use find function of std::string.
```
size_t find(const std::string& str)
//it will return the first position of the first substring found in the current string.
```
So that if find return 0, it means that the pref string is the preffix of words[i], then incrementing cnt.

# Complexity🚀
- Time complexity: O(n.m) ⏱️
n is for the loop, m is for find function
- Space complexity: O(1) 🗂️

![image.png](https://assets.leetcode.com/users/images/4870b025-8589-4b9c-be10-25033e3b6272_1736393443.3972106.png)


# version 2: Substring matching
A loop for checking all strings in the array and use find function of std::string.
```
std::string substr(size_t pos = 0, size_t len = npos) const
//return the substring that start from pos, with length 'len'
```
So that if we check substring of words[i], which start from index 0 and having `pref.size()`, if that substring match pref string, then incrementing cnt.


# Complexity🚀
- Time complexity: O(n.p) ⏱️
n is for the loop, and p is for extracting a substring of size p.

- Space complexity: O(p) 🗂️
The `substr` function create a temporary substring of size p for each word in the vector.

![image.png](https://assets.leetcode.com/users/images/43ce7899-ced9-49b3-b976-38a5d2a1ee82_1736393478.6542516.png)


# Version 1
```cpp []
class Solution {
public:
    int prefixCount(vector<string>& words, string pref) {
        int cnt = 0;
        for(int i = 0; i < words.size(); i++){
            if(words[i].find(pref) == 0){
                cnt++;
            }
        }
        return cnt;
    }
};
```

# Version 2

```cpp []
class Solution {
public:
    int prefixCount(vector<string>& words, string pref) {
        int cnt = 0;
        for(int i = 0; i < words.size(); i++){
            if(words[i].substr(0, pref.size()) == pref){
                cnt++;
            }
        }
        return cnt;
    }
};
```
![image.png](https://assets.leetcode.com/users/images/d1172eac-d39a-49bf-9591-0705444807a2_1736393523.377854.png)

['String', 'String Matching', 'C++']