✅ Beginner Friendly | Easy | Beats 100% | Detailed Video Explanation🔥
IntuitionThe problem involves counting words that start with a given prefix. My first thought was to iterate through the list of words and check each one to see
# Intuition
The problem involves counting words that start with a given prefix. My first thought was to iterate through the list of words and check each one to see if it begins with the specified prefix. This can be achieved efficiently using the `startsWith` method provided in Java's `String` class.

# Approach
1. Initialize a counter to keep track of the words that start with the given prefix.
2. Iterate through the array of words.
3. For each word, use the `startsWith` method to check if it begins with the prefix.
4. If the condition is true, increment the counter.
5. Return the counter after processing all the words.

This approach is simple and leverages Java's built-in method for prefix matching, ensuring clarity and correctness.

# Complexity
- Time complexity:
  The time complexity is $$O(n \cdot m)$$, where:
  - $$n$$ is the number of words in the array.
  - $$m$$ is the length of the prefix.
  Each word requires a comparison of its first $$m$$ characters with the prefix.

- Space complexity:
  The space complexity is $$O(1)$$ because no extra space is used apart from a few variables for iteration and counting.


# Code
```java []
class Solution {
    // Method to count the number of words in the array `words` that start with the prefix `pref`
    public int prefixCount(String[] words, String pref) {
        // Get the total number of words in the input array
        int n = words.length;
        
        // Initialize a counter to track the number of words starting with the given prefix
        int count = 0;
        
        // Iterate through each word in the array
        for (int i = 0; i < n; i++) {
            // Access the current word from the array
            String word = words[i];
            
            // Check if the current word starts with the specified prefix
            if (word.startsWith(pref)) {
                // Increment the counter if the condition is true
                count++;
            }
        }

        // Return the total count of words starting with the prefix
        return count;
    }
}

```

LeetCode 2185 Counting Words With a Given Prefix | Easy | String Matching | Asked in Google
https://youtu.be/sbDVQcs0hhg
['Array', 'String', 'String Matching', 'Java']