Count Matching Pattern Entries

Quick Overview

Count Matching Pattern Entries. For each string in patterns, check whether it occurs contiguously inside word. Return the number of matching array entries. Repeated pattern entries count independently, but one entry contributes at most one regardless of its number of occurrences.

Count Matching Pattern Entries

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Return how many array entries in `patterns` occur as contiguous substrings of `word`. Repeated entries in the array count separately. An individual entry contributes one when it occurs at least once, regardless of how many matching positions it has. ```cpp class Solution { public: int numOfStrings(vector<string>& patterns, string word); }; ``` ### Examples **Example 1:** ```text patterns = ["an","na","x","an"], word = "banana" Output: 3 Explanation: Both entries "an" and the entry "na" match; "x" does not. ``` **Example 2:** ```text patterns = ["cat","at","tac"], word = "cat" Output: 2 Explanation: The whole word and its suffix "at" match. ``` ### Constraints - `1 <= patterns.length <= 100`. - Pattern lengths and word length lie in `[1,100]`. - All characters are lowercase English letters. ### Clarifying Questions This tests substring existence, not subsequence membership or number of occurrences. ```hint Inspect the structure What should happen when two array entries have identical text? ``` ### Reasoning checkpoints - Count pattern entries rather than matching locations, retaining the multiplicity of repeated array entries. - Apply contiguous substring matching, including whole-word matches and patterns longer than the word. ### Optional extensions How could you answer many large batches against the same unchanged word more efficiently?

Overview: Count Matching Pattern Entries. For each string in patterns, check whether it occurs contiguously inside word. Return the number of matching array entries. Repeated pattern entries count independently, but one entry contributes at most one regardless of its number of occurrences.

Google logo
Google
May 27, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return how many array entries in patterns occur as contiguous substrings of word. Repeated entries in the array count separately. An individual entry contributes one when it occurs at least once, regardless of how many matching positions it has.

class Solution {
public:
    int numOfStrings(vector<string>& patterns, string word);
};

Examples

Example 1:

patterns = ["an","na","x","an"], word = "banana"
Output: 3
Explanation: Both entries "an" and the entry "na" match; "x" does not.

Example 2:

patterns = ["cat","at","tac"], word = "cat"
Output: 2
Explanation: The whole word and its suffix "at" match.

Constraints

  • 1 <= patterns.length <= 100 .
  • Pattern lengths and word length lie in [1,100] .
  • All characters are lowercase English letters.

Clarifying Questions Guidance

This tests substring existence, not subsequence membership or number of occurrences.

Reasoning checkpoints

  • Count pattern entries rather than matching locations, retaining the multiplicity of repeated array entries.
  • Apply contiguous substring matching, including whole-word matches and patterns longer than the word.

Optional extensions

How could you answer many large batches against the same unchanged word more efficiently?