Skip to content

1967. Number of Strings That Appear as Substrings in Word 👍

  • Time: $O(n\log^* n)$
  • Space: $O(n)$
1
2
3
4
5
6
7
8
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;
    });
  }
};
1
2
3
4
5
class Solution {
  public int numOfStrings(String[] patterns, String word) {
    return (int) Arrays.stream(patterns).filter(pattern -> word.contains(pattern)).count();
  }
}
1
2
3
class Solution:
  def numOfStrings(self, patterns: list[str], word: str) -> int:
    return sum(pattern in word for pattern in patterns)