Java One line Solution||Beats 💯✅✅
IntuitionThe task is to count how many words in the array start with a specific prefix. The natural way to solve this is to iterate through each word and check


### **Intuition**
The task is to count how many words in the array start with a specific prefix. The natural way to solve this is to iterate through each word and check if the prefix matches the beginning of the word. Java provides a built-in method, `String.startsWith`, which simplifies this process and ensures correctness.

---

### **Approach**
1. **Iterate through the list of words**:
   - Use a `for` loop to examine each word in the `words` array.
2. **Check if the word starts with the prefix**:
   - Use `word.startsWith(pref)` to determine if the word begins with the given prefix `pref`.
3. **Increment the count**:
   - If the word matches, increase the count by 1.
4. **Return the count**:
   - After processing all the words, return the final count.

---

### **Complexity**
- **Time Complexity**:  
  - $$O(n \cdot m)$$, where \(n\) is the number of words in the array, and \(m\) is the length of the prefix.  
    - We iterate through each word (\(O(n)\)).
    - For each word, we check if it starts with the prefix (\(O(m)\)).

- **Space Complexity**:  
  - $$O(1)$$.  
    - No additional space is used; we only maintain a counter.

---

### **Edge Cases**
1. **Empty prefix** (`pref = ""`):  
   - Every word matches an empty prefix, so the count equals the number of words in the array.
2. **Empty array** (`words = []`):  
   - The result is `0` since there are no words to check.
3. **No matches**:  
   - If no word starts with the prefix, the result is `0`.

---

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

---

### **Example Walkthrough**

#### Input:
```text
words = ["apple", "apricot", "banana", "apex"]
pref = "ap"
```

#### Execution:
1. Check `"apple"`: Starts with `"ap"`. Count = 1.
2. Check `"apricot"`: Starts with `"ap"`. Count = 2.
3. Check `"banana"`: Does not start with `"ap"`. Count = 2.
4. Check `"apex"`: Starts with `"ap"`. Count = 3.

#### Output:
```text
3
```

![image.png](https://assets.leetcode.com/users/images/52597733-42e6-4220-9ff9-f6475fd13da8_1736399546.3465905.png)



['Java']