Best Code with Optimum Complexity 🚀
IntuitionThe problem involves checking if a prefix exists in a set of strings. Since we only need to compare the prefix with the beginning of each word, we can
# Intuition
The problem involves checking if a prefix exists in a set of strings. Since we only need to compare the prefix with the beginning of each word, we can iterate through the array and check each word for the required condition.

# Approach
Iterate through each word in the array.
Use the startsWith() method of the String class to check if the current word begins with the given prefix.
If the condition is true, increment a counter to keep track of the number of matches.
Return the counter after iterating through the array.
This approach is straightforward and leverages Java's built-in startsWith() method for efficiency and simplicity.
# Complexity
Time Complexity:

The time complexity is O(n * m), where:

n: Number of words in the array (words.length).

m: Length of the prefix (pref.length), since startsWith checks up to the length of the prefix for each word.


Space Complexity:
The space complexity is O(1) because we use only a constant amount of extra space for the counter.

# Code
```java []
class Solution {
    public int prefixCount(String[] words, String pref) {
        int num=0;
        for(String word:words){
            int i;
            if(word.startsWith(pref)){
                num++;
            }
        }
        return num;
    }
}
```
['Java']