Number of Strings That Appear as Substrings in Word - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: patterns = ["a","aa","aaa"], word = "aaaa"
Output: 3
💡 Note: All patterns are substrings: "a" appears at positions 0,1,2,3; "aa" appears at positions 0,1,2; "aaa" appears at positions 0,1
Example 2 — Partial Matches
$ Input: patterns = ["a","b","c"], word = "aaaaabbbbb"
Output: 2
💡 Note: "a" and "b" are found in word, but "c" is not present
Example 3 — No Matches
$ Input: patterns = ["a","a","a"], word = "ab"
Output: 3
💡 Note: All three patterns "a" exist in word "ab", even though they're duplicates

Constraints

  • 1 ≤ patterns.length ≤ 100
  • 1 ≤ patterns[i].length ≤ 100
  • 1 ≤ word.length ≤ 100
  • patterns[i] and word consist of lowercase English letters.

Visualization

Tap to expand
Substring Count Problem INPUT patterns[] "a" "aa" "aaa" [0] [1] [2] word "aaaa" 0 1 2 3 3 patterns to check word length: 4 ALGORITHM STEPS 1 Initialize counter count = 0 2 Loop through patterns for each pattern in array 3 Check substring if pattern in word 4 Increment if found count += 1 Substring Checks: "a" in "aaaa" OK "aa" in "aaaa" OK "aaa" in "aaaa" OK FINAL RESULT All patterns found in word! "a" found at 0,1,2,3 "aa" found at 0,1,2 "aaa" found at 0,1 Total matches: 3/3 Output 3 3 patterns are substrings Key Insight: Built-in substring search methods (like Python's 'in' operator or Java's contains()) provide O(n*m) time complexity per check. Total: O(k * n * m) where k=patterns count, n=word length, m=avg pattern length. TutorialsPoint - Number of Strings That Appear as Substrings in Word | Built-in Substring Search
Asked in
Amazon 15 Google 12 Microsoft 8
25.0K Views
Medium Frequency
~5 min Avg. Time
892 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