---
# System prepended metadata

title: 2185. Counting Words With a Given Prefix
tags: [Leetcode, Easy]

---

# 2185. Counting Words With a Given Prefix
###### tags: `Leetcode` `Easy`
Link: https://leetcode.com/problems/counting-words-with-a-given-prefix/description/
## 思路
```word.indexOf()```returns the position of the first occurrence of specified character(s) in a string
一个follow up:
Q:如果给的words是sorted过的怎么办?
A:可以用binary search 时间复杂度O(klogN)
## Code
```java=
class Solution {
    public int prefixCount(String[] words, String pref) {
        int ans = 0;
        for(String word:words){
            if(word.indexOf(pref)==0) ans++;
        }
        return ans;
    }
}
```