Number of Strings That Appear as Substrings in Word (LeetCode 1967): Solution and Explanation
Count how many patterns occur inside word. Each pattern is checked on its own with a substring search, and duplicates are counted every time they appear.
Intuition
The patterns do not interact, so the answer is just the number of patterns that pass one test: does this pattern occur somewhere in word?
That test is a substring search. Picture the pattern as a stencil sliding along word one position at a time. If at some offset every letter lines up, the pattern occurs.
With every string at most 100 letters long, the built-in search of any language is more than fast enough.
Approach: Check each pattern
- Set count = 0.
- For each pattern p, check whether p occurs in word (p in word, contains, includes, find).
- If it does, add 1 to count.
- Return count.
Step-by-step example
patterns = ["a","abc","bc","d"], word = "abc"
- "a" lines up at offset 0. count = 1.
- "abc" lines up at offset 0 and covers the whole word. count = 2.
- "bc" fails at offset 0 (b against a) and lines up at offset 1. count = 3.
- "d" matches at no offset. count stays 3.
- Return 3.
Code: Python, Java, C++
1class Solution:
2 def numOfStrings(self, patterns: List[str], word: str) -> int:
3 count = 0
4 for p in patterns:
5 if p in word:
6 count += 1
7 return count1class Solution {
2 public int numOfStrings(String[] patterns, String word) {
3 int count = 0;
4 for (String p : patterns) {
5 if (word.contains(p)) {
6 count++;
7 }
8 }
9 return count;
10 }
11}1class Solution {
2public:
3 int numOfStrings(vector<string>& patterns, string word) {
4 int count = 0;
5 for (const string& p : patterns) {
6 if (word.find(p) != string::npos) {
7 count++;
8 }
9 }
10 return count;
11 }
12};Time and space complexity
O(P * n * m)
P patterns, each searched in word of length n with patterns up to length m using a simple search. With all lengths at most 100 this is tiny.
O(1)
Only the counter is stored.
Other approaches
KMP per pattern (time O(P * (n + m)), space O(m))
Build the failure table of each pattern and scan word in linear time. Worth it only for much longer strings.
Set of all substrings of word (time O(n^3 + P * m), space O(n^3))
Put every substring of word into a hash set, then check each pattern by lookup. It helps when there are many patterns and word is short.
Aho-Corasick (time O(total pattern length + n), space O(total pattern length))
Build one automaton from all patterns and scan word once. Overkill here, but the standard tool for many patterns over a long text.
Common mistakes
- Counting distinct patterns instead of every entry. ["a","a","a"] with word "ab" gives 3.
- Checking whether word is inside the pattern instead of the pattern inside word.
- Checking subsequences instead of substrings. The letters must be contiguous.
Frequently asked questions
Do duplicate patterns count more than once?
Yes. The answer counts entries of the array, so each copy that occurs in word adds 1.
What if a pattern is longer than word?
It cannot occur, and every substring search returns false for it right away.
Is the built-in substring search acceptable?
Yes. With lengths up to 100, the built-in search is the expected solution. Mention KMP or Aho-Corasick if asked how to scale it.