Back to problems Sign in to unlock
Leetcode
Easy
Arrays
Strings
Counting Words With A Given Prefix
Count how many words in a list start with a given prefix.
Acceptance 0%
Problem Statement
Problem
You are given a list of words and a prefix string. Return how many words begin with that prefix.
A word is considered a match if the prefix appears at the start of the word exactly, character by character.
Goal
Write a function that counts the matching words efficiently and returns the total number of matches.
Input Format
- A list of strings
words - A string
prefrepresenting the prefix to match
Output Format
- Return a single integer: the number of words in
wordsthat start withpref
Constraints
1 <= words.length- Each word and the prefix contain lowercase English letters
- The prefix may be empty in generalized settings, but for interview practice assume it is non-empty unless stated otherwise
Examples
Sample cases returned by the problem API.
Example 1
Input
words = ["pay", "attention", "practice", "attend"], pref = "at"
Output
2
Explanation
"attention" and "attend" start with "at".
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.