leetcodedaybyday - Beats 100% with C++ and Beats 100% with Python3
IntuitionThe task is to count how many words in the words list are prefixes of the string s. A prefix of a string is defined as a substring that starts at the b
# Intuition
The task is to count how many words in the `words` list are prefixes of the string `s`. A prefix of a string is defined as a substring that starts at the beginning of the string. To solve this, we can compare each word in the `words` list with the corresponding prefix of `s` of the same length.

# Approach
1. **Iterate Through Words**:
   - For each word in the `words` list, check if it matches the prefix of `s` with the same length as the word.
   - In Python, we can use the `startswith` method to check for prefixes efficiently.
   - In C++, use the `substr` method to extract the prefix of `s` and compare it with the word.

2. **Count Matches**:
   - If the word matches the prefix, increment the prefix counter.

3. **Return the Count**:
   - At the end of the loop, return the counter as the result.

# Complexity
- **Time Complexity**:  
  - `O(n * m)`, where `n` is the number of words in the `words` list and `m` is the average length of the words.
  - For each word, we check its length against the prefix of `s`, which takes `O(m)` time.
  
- **Space Complexity**:  
  - `O(1)`. No additional space is used apart from a counter.

# Code
```cpp []
class Solution {
public:
    int countPrefixes(vector<string>& words, string s) {
        int prefixCount = 0;
        for (string word : words){
            if (word == s.substr(0, word.size())){
                prefixCount++;
            }
        }
        return prefixCount;
    }
};


```
```python3 []
class Solution:
    def countPrefixes(self, words: List[str], s: str) -> int:
        prefixCount = 0
        # for word in words:
        #     if word == s[:len(word)]:
        #         prefixCount += 1
        # return prefixCount
        for word in words:
            if s.startswith(word):
                prefixCount += 1
        return prefixCount



```

['Array', 'String', 'C++', 'Python3']