Number of Strings That Appear as Substrings in Word - Problem

You're given an array of pattern strings and a target word. Your task is to count how many of these patterns actually appear as substrings within the target word.

A substring is a contiguous sequence of characters within a string. For example, "cat" is a substring of "concatenate", but "cta" is not (since the characters aren't contiguous).

Goal: Return the total count of pattern strings that exist as substrings in the target word.

Input: An array of strings patterns and a string word

Output: An integer representing the count of matching patterns

Input & Output

example_1.py โ€” Basic Case
$ Input: patterns = ["a", "aa", "aaa"], word = "aaaaaaa"
โ€บ Output: 3
๐Ÿ’ก Note: All three patterns exist as substrings: 'a' appears multiple times, 'aa' appears multiple times, and 'aaa' also appears multiple times in the word 'aaaaaaa'.
example_2.py โ€” Mixed Case
$ Input: patterns = ["a", "b", "c"], word = "aaaaabbbbb"
โ€บ Output: 2
๐Ÿ’ก Note: Only 'a' and 'b' exist as substrings in 'aaaaabbbbb'. The pattern 'c' does not appear anywhere in the word.
example_3.py โ€” No Matches
$ Input: patterns = ["a", "aa", "aaa"], word = "bbbbbbbb"
โ€บ Output: 0
๐Ÿ’ก Note: None of the patterns containing 'a' characters appear in the word 'bbbbbbbb', so the count is 0.

Visualization

Tap to expand
Pattern Matching ProcessTarget Word:"aaaaaaa"Patterns to Find:"a""aa""aaa"Search Results:โœ“ "a" foundโœ“ "aa" foundโœ“ "aaa" foundMatching Process:1. Check "a" in "aaaaaaa"Found at multiple positions โœ“2. Check "aa" in "aaaaaaa"Found at positions 0,1,2,3,4,5 โœ“3. Check "aaa" in "aaaaaaa"Found at positions 0,1,2,3,4 โœ“Algorithm Benefits:โ€ข Uses optimized built-in methodsโ€ข Simple and readable codeโ€ข Handles edge cases automaticallyFinal Result:3patterns foundComplexity:Time: O(n ร— m)Space: O(1)n = number of patternsm = length of word๐ŸŽฏ All 3 patterns successfully matched!
Understanding the Visualization
1
Load Patterns
Start with array of patterns to search for: ['a', 'aa', 'aaa']
2
Check Each Pattern
For each pattern, use optimized search to find if it exists in target word
3
Count Matches
Increment counter for each pattern found as substring
4
Return Result
Return total count of patterns that exist as substrings
Key Takeaway
๐ŸŽฏ Key Insight: Use built-in string search methods instead of manual character comparison - they're optimized, handle edge cases, and result in much cleaner code!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * m)

Where n is the number of patterns and m is the length of the word. Built-in methods are optimized and average much better than brute force.

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a counter variable, no additional data structures needed

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค patterns.length โ‰ค 100
  • 1 โ‰ค patterns[i].length โ‰ค 100
  • 1 โ‰ค word.length โ‰ค 100
  • patterns[i] and word consist of lowercase English letters only
Asked in
Amazon 25 Google 18 Meta 15 Microsoft 12
42.0K Views
Medium Frequency
~8 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen