Number of Strings That Appear as Substrings in Word (LeetCode 1967): Solution and Explanation

Easy
Array
String

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

  1. Set count = 0.
  2. For each pattern p, check whether p occurs in word (p in word, contains, includes, find).
  3. If it does, add 1 to count.
  4. Return count.

Step-by-step example

patterns = ["a","abc","bc","d"], word = "abc"
  1. "a" lines up at offset 0. count = 1.
  2. "abc" lines up at offset 0 and covers the whole word. count = 2.
  3. "bc" fails at offset 0 (b against a) and lines up at offset 1. count = 3.
  4. "d" matches at no offset. count stays 3.
  5. 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 count
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

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.

Space

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.