Given an array of strings patterns and a string word, return the number of strings in patterns that exist as a substring in word.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: patterns = ["a","abc","bc","d"], word = "abc" Output: 3 Explanation: - "a" appears as a substring in "abc". - "abc" appears as a substring in "abc". - "bc" appears as a substring in "abc". - "d" does not appear as a substring in "abc". 3 of the strings in patterns appear as a substring in word.
Example 2:
Input: patterns = ["a","b","c"], word = "aaaaabbbbb" Output: 2 Explanation: - "a" appears as a substring in "aaaaabbbbb". - "b" appears as a substring in "aaaaabbbbb". - "c" does not appear as a substring in "aaaaabbbbb". 2 of the strings in patterns appear as a substring in word.
Example 3:
Input: patterns = ["a","a","a"], word = "ab" Output: 3 Explanation: Each of the patterns appears as a substring in word "ab".
Constraints:
1 <= patterns.length <= 1001 <= patterns[i].length <= 1001 <= word.length <= 100patterns[i] and word 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 approach to find substrings involves checking every possible small word to see if it is inside the larger word. It’s like manually searching through a book to see if you can find each of the smaller words somewhere within the bigger word.
Here's how the algorithm would work step-by-step:
def number_of_strings_that_appear_as_substrings_in_word(patterns, word):
number_of_matches = 0
for pattern in patterns:
# Iterate through each pattern in the list of patterns
for index in range(len(word) - len(pattern) + 1):
if word[index:index + len(pattern)] == pattern:
# Check if the pattern matches the substring in the main word
number_of_matches += 1
# No need to keep checking this pattern
break
return number_of_matchesThe best way to solve this is to directly check if each small string is a part of the big string. We can do this without looking at every single possible starting position in the big string. We will iterate through the small strings and use a built-in function to verify if a string is contained in another.
Here's how the algorithm would work step-by-step:
def number_of_strings_that_appear_as_substrings_in_word(small_strings, big_string):
number_of_small_strings_found = 0
# We iterate through each small string to check if its a substring
for current_small_string in small_strings:
# Check if the current small string is a substring of the big string
if current_small_string in big_string:
number_of_small_strings_found += 1
# Return the total count of small strings found in the big string
return number_of_small_strings_found| Case | How to Handle |
|---|---|
| patterns is null or empty | Return 0, as no patterns can be substrings of word. |
| word is null or empty | Return 0, as no substrings can exist in an empty word. |
| patterns contains an empty string | Count occurrences of empty string as a substring of word, which means it appears 1 time. |
| word is a very long string (e.g., 10^5 characters) | Ensure algorithm scales linearly or better, avoiding quadratic or worse time complexity. |
| patterns contains duplicate strings | Count each unique string only once, even if it appears multiple times in patterns. |
| patterns contains strings longer than word | These strings can never be substrings, so ignore them. |
| All patterns are identical and substrings of word | Count the pattern only once. |
| No patterns are substrings of word | Return 0, indicating no matches found. |