09.01.2025
2185. Counting Words With a Given Prefix easy blog post substack youtube deep-dive
Join me on Telegram
Problem TLDR
Count words with prefix #easy
Intuition
Brute-force is optimal.
Approach
how short can it be?
Complexity
Time complexity: $$O(n)$$
Space complexity: $$O(1)$$
Code
fun prefixCount(words: Array<String>, pref: String) =
words.count { it.startsWith(pref) }
pub fn prefix_count(words: Vec<String>, pref: String) -> i32 {
words.iter().filter(|w| w.starts_with(&pref)).count() as _
}
int prefixCount(vector<string>& words, string pref) {
int r = 0;
for (auto &w: words) r += w.starts_with(pref);
return r;
}