🚀 100% Efficient Solution: Optimal Prefix Counter with Clarity & Speed! 🌟 || Approved ✅
IntuitionTo solve this problem, we need to count how many words in the given list start with the specified prefix. This can be efficiently done by iterating ove
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
To solve this problem, we need to count how many words in the given list start with the specified prefix. This can be efficiently done by iterating over the list and checking if each word begins with the prefix.

![Screenshot 2025-01-09 081445.png](https://assets.leetcode.com/users/images/1a4d7daa-446a-4bbb-a732-f4c07518689c_1736391024.0580993.png)
https://leetcode.com/u/Hari_Veera_Venkat/


# Approach
<!-- Describe your approach to solving the problem. -->
1. **Iterate Through the List:**  
   - Use a loop to iterate through each word in the input list `words`.  
2. **Check Prefix:**  
   - Skip the word if its length is less than the length of the prefix, as it cannot contain the prefix.  
   - Use the `substr` function to extract the prefix-length substring of the current word and compare it to the given prefix.  
3. **Increment Counter:**  
   - If the word starts with the prefix, increment the result counter `res`.  
4. **Return Result:**  
   - After checking all words, return the value of `res`.


# Complexity
- **Time Complexity:**  
  $$O(n \cdot k)$$  
  - \(n\): Number of words in the list.  
  - \(k\): Length of the prefix.  
  - For each word, we perform at most \(O(k)\) operations to extract and compare the prefix.  

- **Space Complexity:**  
  $$O(1)$$  
  - No additional data structures are used apart from variables for counters.


# Code
```c []
int prefixCount(char** words, int wordsSize, char* pref) {
    int count = 0;
    for (int i = 0; i < wordsSize; i++) {
        if (strncmp(words[i], pref, strlen(pref)) == 0) {
            count++;
        }
    }
    return count;
}
```
```cpp []
class Solution {
public:
    int prefixCount(vector<string>& words, string pref) {
        int res = 0;
        for (int i = 0; i < words.size(); i++) {
            if (words[i].size() < pref.size())
                continue;
            if (words[i].substr(0, pref.size()) == pref)
                res++;
        }
        return res;
    }
};
```
```python []
class Solution(object):
    def prefixCount(self, words, pref):
        res=0;
        for word in words:
            if  len(word) >= len(pref) and word[:len(pref)] ==  pref:
                res+=1;
        return res;
```
```python3 []
class Solution:
    def prefixCount(self, words: List[str], pref: str) -> int:
        res = 0
        for word in words:
            if word.startswith(pref):
                res += 1
        return res
```
```java []
class Solution {
    public int prefixCount(String[] words, String pref) {
        int res = 0;
        for (String word : words) {
            if (word.length() >= pref.length() && word.substring(0, pref.length()).equals(pref)) {
                res++;
            }
        }
        return res;
    }
}
```

# Key Points
- **Efficient Filtering:** Skips unnecessary checks by comparing word lengths with the prefix upfront.  
- **Direct Comparison:** Uses `substr` to directly extract and compare prefixes, ensuring simplicity and clarity.  
- **Handles Edge Cases:** Works seamlessly for words shorter than the prefix or when no matches exist.  
- **Optimal for Large Inputs:** The solution is designed to handle large datasets efficiently.  
- **Clean & Readable Code:** Well-structured logic makes it easy to understand and maintain.  

🌟 **Why You'll Love This Solution!**  
- **Clear & Simple 💡:** Designed for maximum readability.  
- **100% Efficiency Approved 🏆:** Handles large datasets effectively.  
- **Reusable 🎨:** Works with any list of words and prefix combinations.  

💖 **Show Your Support!** 👍  
If this solution helped you, hit that like button and drop your feedback below. Your encouragement motivates more optimized solutions! 🌍✨


['C', 'Python', 'C++', 'Java', 'Python3']