Problem

Given an array of strings patterns and a string word, return _thenumber of strings in _patterns _that exist as asubstring in _word.

A substring is a contiguous sequence of characters within a string.

Examples

Example 1

1
2
3
4
5
6
7
8
Input: patterns = ["a","abc","bc","d"], word = "abc"
Output: 3
Explanation:
- "a" appears as a substring in "_a_ bc".
- "abc" appears as a substring in "_abc_ ".
- "bc" appears as a substring in "a _bc_ ".
- "d" does not appear as a substring in "abc".
3 of the strings in patterns appear as a substring in word.

Example 2

1
2
3
4
5
6
7
Input: patterns = ["a","b","c"], word = "aaaaabbbbb"
Output: 2
Explanation:
- "a" appears as a substring in "a _a_ aaabbbbb".
- "b" appears as a substring in "aaaaabbbb _b_ ".
- "c" does not appear as a substring in "aaaaabbbbb".
2 of the strings in patterns appear as a substring in word.

Example 3

1
2
3
Input: patterns = ["a","a","a"], word = "ab"
Output: 3
Explanation: Each of the patterns appears as a substring in word "_a_ b".

Constraints

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

Solution

Method 1 – Simple Substring Check

Intuition

For each pattern, check if it appears as a substring in the given word. This is a direct application of the substring operation and is efficient given the constraints.

Approach

  1. Initialize a counter ans to 0.
  2. For each string in patterns, check if it is a substring of word.
  3. If yes, increment ans.
  4. Return ans.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
public:
    int numOfStrings(vector<string>& patterns, string word) {
        int ans = 0;
        for (const auto& p : patterns) {
            if (word.find(p) != string::npos) ++ans;
        }
        return ans;
    }
};
1
2
3
4
5
6
7
8
9
func numOfStrings(patterns []string, word string) int {
    ans := 0
    for _, p := range patterns {
        if strings.Contains(word, p) {
            ans++
        }
    }
    return ans
}
1
2
3
4
5
6
7
8
9
class Solution {
    public int numOfStrings(String[] patterns, String word) {
        int ans = 0;
        for (String p : patterns) {
            if (word.contains(p)) ans++;
        }
        return ans;
    }
}
1
2
3
4
5
6
7
8
9
class Solution {
    fun numOfStrings(patterns: Array<String>, word: String): Int {
        var ans = 0
        for (p in patterns) {
            if (word.contains(p)) ans++
        }
        return ans
    }
}
1
2
3
4
5
6
7
class Solution:
    def numOfStrings(self, patterns: list[str], word: str) -> int:
        ans = 0
        for p in patterns:
            if p in word:
                ans += 1
        return ans
1
2
3
4
5
impl Solution {
    pub fn num_of_strings(patterns: Vec<String>, word: String) -> i32 {
        patterns.iter().filter(|p| word.contains(&**p)).count() as i32
    }
}
1
2
3
4
5
6
7
8
9
class Solution {
    numOfStrings(patterns: string[], word: string): number {
        let ans = 0;
        for (const p of patterns) {
            if (word.includes(p)) ans++;
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n * m * w), where n is the number of patterns, m is the average length of a pattern, and w is the length of word. Each substring check is up to O(m * w).
  • 🧺 Space complexity: O(1), as we use only a constant amount of extra space.