Number of Strings That Appear as Substrings in Word

You can implement this by iterating through the patterns and checking if each pattern exists as a substring in the given word. If a pattern is found as a substring in the word, you can increment a count. Finally, return the count.

Here’s the code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
    def numOfStrings(self, patterns: List[str], word: str) -> int:
        count = 0 # Counter to keep track of the number of substrings found

        # Iterate through each pattern in the patterns list
        for pattern in patterns:
            # Check if the pattern is a substring in the word
            if pattern in word:
                # If yes, increment the count
                count += 1

        # Return the final count
        return count

This code loops through each pattern and uses the in operator to check if it’s a substring of word. The time complexity of this solution is O(n * m), where n is the length of patterns and m is the length of word.