Skip to main content
Back to problems
#2185
Easy Algorithms

Counting words with a given prefix

Array String String Matching
84.5% acceptance
Feb 25, 2026
1122
38
You are given an array of strings words and a string pref. Return the number of strings in words that contain pref as a prefix.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn prefix_count(words: Vec<String>, pref: String) -> i32 {
    words.iter().filter(|w| w.starts_with(&pref as &str)).count() as i32
  }
}