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 a string array words and a string s, where words[i] and s comprise only of lowercase English letters.
Return the number of strings in words that are a prefix of s.
A prefix of a string is a substring that occurs at the beginning of the string. A substring is a contiguous sequence of characters within a string.
Example 1:
Input: words = ["a","b","c","ab","bc","abc"], s = "abc" Output: 3 Explanation: The strings in words which are a prefix of s = "abc" are: "a", "ab", and "abc". Thus the number of strings in words which are a prefix of s is 3.
Example 2:
Input: words = ["a","a"], s = "aa" Output: 2 Explanation: Both of the strings are a prefix of s. Note that the same string can occur multiple times in words, and it should be counted each time.
Constraints:
1 <= words.length <= 10001 <= words[i].length, s.length <= 10words[i] and s consist of lowercase English letters only.Problem summary: You are given a string array words and a string s, where words[i] and s comprise only of lowercase English letters. Return the number of strings in words that are a prefix of s. A prefix of a string is a substring that occurs at the beginning of the string. A substring is a contiguous sequence of characters within a string.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
["a","b","c","ab","bc","abc"] "abc"
["a","a"] "aa"
check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence)check-if-string-is-a-prefix-of-array)counting-words-with-a-given-prefix)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2255: Count Prefixes of a Given String
class Solution {
public int countPrefixes(String[] words, String s) {
int ans = 0;
for (String w : words) {
if (s.startsWith(w)) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2255: Count Prefixes of a Given String
func countPrefixes(words []string, s string) (ans int) {
for _, w := range words {
if strings.HasPrefix(s, w) {
ans++
}
}
return
}
# Accepted solution for LeetCode #2255: Count Prefixes of a Given String
class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
return sum(s.startswith(w) for w in words)
// Accepted solution for LeetCode #2255: Count Prefixes of a Given String
impl Solution {
pub fn count_prefixes(words: Vec<String>, s: String) -> i32 {
words.iter().filter(|w| s.starts_with(w.as_str())).count() as i32
}
}
// Accepted solution for LeetCode #2255: Count Prefixes of a Given String
function countPrefixes(words: string[], s: string): number {
return words.filter(w => s.startsWith(w)).length;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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.