Intuition¶
The task is just to count how many of the given patterns are substrings of
word. Since the inputs are tiny (everything <= 100), a direct substring check
per pattern is more than fast enough — no preprocessing or suffix structure
needed.
Approach: Direct substring check¶
Iterate over each pattern and test whether it occurs in word using the
language's built-in substring search (String::contains in Rust,
strings.Contains in Go). Increment a counter for every pattern that matches and
return the total.
Complexity¶
- Time complexity: $$O(p \cdot L \cdot m)$$, where
pis the number of patterns,Lis the length ofword, andmis the longest pattern length — each of thepsubstring searches is $$O(L \cdot m)$$ in the worst case. - Space complexity: $$O(1)$$ extra space (only a counter is kept).
Code¶
Go¶
import "strings"
func numOfStrings(patterns []string, word string) int {
count := 0
for _, pattern := range patterns {
if strings.Contains(word, pattern) {
count++
}
}
return count
}