1967. Number of Strings That Appear as Substrings in Word LeetCode Solution

In this guide, you will get 1967. Number of Strings That Appear as Substrings in Word LeetCode Solution with the best time and space complexity. The solution to Number of Strings That Appear as Substrings in Word problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Number of Strings That Appear as Substrings in Word solution in C++
  4. Number of Strings That Appear as Substrings in Word solution in Java
  5. Number of Strings That Appear as Substrings in Word solution in Python
  6. Additional Resources
1967. Number of Strings That Appear as Substrings in Word LeetCode Solution image

Problem Statement of Number of Strings That Appear as Substrings in Word

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.

Example 1:

Input: patterns = [“a”,”abc”,”bc”,”d”], word = “abc”
Output: 3
Explanation:
– “a” appears as a substring in “abc”.
– “abc” appears as a substring in “abc”.
– “bc” appears as a substring in “abc”.
– “d” does not appear as a substring in “abc”.
3 of the strings in patterns appear as a substring in word.

Example 2:

Input: patterns = [“a”,”b”,”c”], word = “aaaaabbbbb”
Output: 2
Explanation:
– “a” appears as a substring in “aaaaabbbbb”.
– “b” appears as a substring in “aaaaabbbbb”.
– “c” does not appear as a substring in “aaaaabbbbb”.
2 of the strings in patterns appear as a substring in word.

See also  3106. Lexicographically Smallest String After Operations With Constraint LeetCode Solution

Example 3:

Input: patterns = [“a”,”a”,”a”], word = “ab”
Output: 3
Explanation: Each of the patterns appears as a substring in word “ab”.

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n\log^* n)
  • Space Complexity: O(n)

1967. Number of Strings That Appear as Substrings in Word LeetCode Solution in C++

class Solution {
 public:
  int numOfStrings(vector<string>& patterns, string word) {
    return ranges::count_if(patterns, [&word](const string& pattern) {
      return word.find(pattern) != word.npos;
    });
  }
};
/* code provided by PROGIEZ */

1967. Number of Strings That Appear as Substrings in Word LeetCode Solution in Java

class Solution {
  public int numOfStrings(String[] patterns, String word) {
    return (int) Arrays.stream(patterns).filter(pattern -> word.contains(pattern)).count();
  }
}
// code provided by PROGIEZ

1967. Number of Strings That Appear as Substrings in Word LeetCode Solution in Python

class Solution:
  def numOfStrings(self, patterns: list[str], word: str) -> int:
    return sum(pattern in word for pattern in patterns)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.