Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
A prefix of a string s is any leading contiguous substring of s.
Example 1:
Input: words = ["pay","attention","practice","attend"], pref = "at"
Output: 2
Explanation: The 2 strings that contain "at" as a prefix are: "attention" and "attend".
Example 2:
Input: words = ["leetcode","win","loops","success"], pref = "code"
Output: 0
Explanation: There are no strings that contain "code" as a prefix.
Constraints:
1 <= words.length <= 1001 <= words[i].length, pref.length <= 100words[i] and pref consist of lowercase English letters.Problem summary: 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. A prefix of a string s is any leading contiguous substring of s.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · String Matching
["pay","attention","practice","attend"] "at"
["leetcode","win","loops","success"] "code"
check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence)count-prefixes-of-a-given-string)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2185: Counting Words With a Given Prefix
class Solution {
public int prefixCount(String[] words, String pref) {
int ans = 0;
for (String w : words) {
if (w.startsWith(pref)) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2185: Counting Words With a Given Prefix
func prefixCount(words []string, pref string) (ans int) {
for _, w := range words {
if strings.HasPrefix(w, pref) {
ans++
}
}
return
}
# Accepted solution for LeetCode #2185: Counting Words With a Given Prefix
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return sum(w.startswith(pref) for w in words)
// Accepted solution for LeetCode #2185: Counting Words With a Given Prefix
impl Solution {
pub fn prefix_count(words: Vec<String>, pref: String) -> i32 {
words.iter().filter(|s| s.starts_with(&pref)).count() as i32
}
}
// Accepted solution for LeetCode #2185: Counting Words With a Given Prefix
function prefixCount(words: string[], pref: string): number {
return words.reduce((r, s) => (r += s.startsWith(pref) ? 1 : 0), 0);
}
Use this to step through a reusable interview workflow for this problem.
At each of the n starting positions in the text, compare up to m characters with the pattern. If a mismatch occurs, shift by one and restart. Worst case (e.g., searching "aab" in "aaaa...a") checks m characters at nearly every position: O(n × m).
KMP and Z-algorithm preprocess the pattern in O(m) to build a failure/Z-array, then scan the text in O(n) — never backtracking. Total: O(n + m). Rabin-Karp uses rolling hashes for O(n + m) expected time. All beat the O(n × m) brute force of checking every position.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.