✅Beats 100% | Simple and Easy Solution | C++| Java | Python | JavaScript
⬆️Upvote if it helps ⬆️Connect with me on Linkedin [Bijoy Sing]IntuitionThe problem requires us to count strings that start with a given prefix. The straightfor
# ⬆️Upvote if it helps ⬆️

---

## Connect with me on Linkedin [Bijoy Sing]

---

```cpp []
class Solution {
public:
    int prefixCount(vector<string>& words, string pref) {
        int count = 0;
        int prefLen = pref.length();
        for (string word : words) {
            if (word.length() >= prefLen) {
                if (word.substr(0, prefLen) == pref) {
                    count++;
                }
            }
        }
        return count;
    }
};
```
```python []
class Solution:
    def prefixCount(self, words: List[str], pref: str) -> int:
        count = 0
        prefLen = len(pref)
        for word in words:
            if len(word) >= prefLen:
                if word[:prefLen] == pref:
                    count += 1
        return count
```
```java []
class Solution {
    public int prefixCount(String[] words, String pref) {
        int count = 0;
        int prefLen = pref.length();
        for (String word : words) {
            if (word.length() >= prefLen) {
                if (word.substring(0, prefLen).equals(pref)) {
                    count++;
                }
            }
        }
        return count;
    }
}
```
```javascript []
var prefixCount = function(words, pref) {
    let count = 0;
    const prefLen = pref.length;
    for (let word of words) {
        if (word.length >= prefLen) {
            if (word.substring(0, prefLen) === pref) {
                count++;
            }
        }
    }
    return count;
};
```
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires us to count strings that start with a given prefix. The straightforward approach is to check each string in the array and verify if it starts with the prefix by comparing characters from the beginning.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Initialize a counter to keep track of strings with the given prefix
2. For each word in the array:
   - First check if the word's length is at least equal to the prefix length
   - If yes, extract a substring from the word with the same length as prefix
   - Compare this substring with the prefix
   - If they match, increment the counter
3. Return the final count

![image.png](https://assets.leetcode.com/users/images/bed6fe42-7701-4803-a5ca-6828c2565888_1736391860.1091683.png)

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$$O(N * K)$$
  - N is the number of words in the input array
  - K is the length of the prefix
  - For each word, we need to check up to K characters to compare with prefix

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
$$O(1)$$
  - We only use a constant amount of extra space
  - The counter variable is the only additional space needed
  - The substring comparisons don't require extra space that grows with input size

### *If you have any questions or need further clarification, feel free to drop a comment! 😊*
['Array', 'String', 'String Matching', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']