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 <= 100
1 <= words[i].length, pref.length <= 100
words[i]
and pref
consist of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to solve this is to look at each word in the list one by one. For each word, we need to see if the prefix matches the beginning of that word.
Here's how the algorithm would work step-by-step:
def count_words_with_prefix_brute_force(list_of_words, prefix):
word_count_with_matching_prefix = 0
for word in list_of_words:
# Check if the prefix matches the start of the word
if word.startswith(prefix):
# Increment the counter because it starts with the prefix
word_count_with_matching_prefix += 1
return word_count_with_matching_prefix
To efficiently count words starting with a given prefix, we avoid checking every word individually. We compare the prefix with the beginning of each word and increment the count only when a match is found.
Here's how the algorithm would work step-by-step:
def count_words_with_prefix(list_of_words, search_prefix):
prefix_count = 0
# Iterate through each word in the input list.
for word in list_of_words:
# Check if the prefix matches the beginning of the word.
if word.startswith(search_prefix):
prefix_count += 1
# Return the total count of words with the given prefix.
return prefix_count
Case | How to Handle |
---|---|
words is null or empty | Return 0 immediately to avoid NullPointerException or incorrect count. |
prefix is null or empty | If prefix is null, throw IllegalArgumentException; if empty, return the length of the words array, as all words have an empty prefix. |
words contains null or empty strings | Skip null or empty strings in words to avoid NullPointerException or incorrect prefix check. |
prefix is longer than any word in words | No words will have that prefix, so the correct count is 0. |
words array contains very long strings | Iterating through long strings could impact performance, so consider optimizing prefix matching with methods like startsWith. |
prefix contains special characters or unicode characters | The startsWith() method should handle unicode characters correctly, but test for unexpected behavior with specific special characters. |
Large words array | Algorithm's time complexity of O(N*M) where N is the size of the array and M is max length of words should be efficient enough, but consider Trie if performance becomes a concern. |
All words in array have the given prefix | The loop should iterate through all words and increment the counter for each, ultimately returning the length of the words array. |