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.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:
We want to find out how many words from a list are prefixes of a given target word. The brute force method involves checking each word in the list to see if it appears at the very beginning of the target word by comparing characters one by one.
Here's how the algorithm would work step-by-step:
def count_prefixes_brute_force(words, target_word): prefix_count = 0
for current_word in words:
is_prefix = True
# If current word is longer than
# the target, it cannot be a prefix
if len(current_word) > len(target_word):
continue
# Iterate through each char to compare
for char_index in range(len(current_word)):
if current_word[char_index] != target_word[char_index]:
is_prefix = False
break
# Increment count when matching prefix
if is_prefix:
prefix_count += 1
return prefix_countThe most efficient way to count prefixes is to build a special data structure that allows for quick prefix lookups. We'll create something like a tree where each branch represents a character, and then use this tree to see how many prefixes of the given string are present in a list of words.
Here's how the algorithm would work step-by-step:
class TrieNode:
def __init__(self):
self.children = {}
self.is_end_of_word = False
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word):
current_node = self.root
for char in word:
if char not in current_node.children:
current_node.children[char] = TrieNode()
current_node = current_node.children[char]
current_node.is_end_of_word = True
def count_prefixes(list_of_words, search_string):
trie = Trie()
for word in list_of_words:
trie.insert(word)
current_node = trie.root
prefix_count = 0
for char in search_string:
if char in current_node.children:
current_node = current_node.children[char]
# Count if the current node marks the end of a word
if current_node.is_end_of_word:
prefix_count += 1
else:
# Stop if no matching character is found
break
return prefix_count| Case | How to Handle |
|---|---|
| Null input string | Throw IllegalArgumentException or return 0 to signal invalid input. |
| Null input prefixes array | Throw IllegalArgumentException or return 0 to signal invalid input. |
| Empty input string | Return the count of empty strings in the prefixes array. |
| Empty prefixes array | Return 0 since there are no prefixes to count. |
| Prefixes array contains null or empty strings | Handle null prefixes by skipping them or throwing an exception; count empty prefixes if the input string is empty or a prefix itself. |
| Very long input string and prefixes array (scalability) | Ensure the solution uses efficient string comparison (e.g., startsWith or similar optimized method) to avoid quadratic time complexity. |
| Prefixes array contains duplicates | The solution should count each duplicate prefix if it appears multiple times and is a prefix of the input string. |
| Input string is a prefix of other strings in the prefixes array | The solution should correctly identify when the whole input string is present as a prefix and count accordingly. |