1967. Number of Strings That Appear as Substrings in Word
UnknownView on LeetCode
Time: O(p * n)
Space: O(1)
Advertisement
Approach
Count patterns where word.Contains(pattern) is true.
Key Techniques
String
String problems range from simple character counting to complex pattern matching. Common approaches include two pointers, sliding window, prefix hashing, and the KMP algorithm. In C#, strings are immutable — use StringBuilder for efficient concatenation inside loops.
1967.cs
C#
// Approach: Count patterns where word.Contains(pattern) is true.
// Time: O(p * n) Space: O(1)
public class Solution
{
public int NumOfStrings(string[] patterns, string word)
{
return patterns.Count(pattern => word.Contains(pattern));
}
}Advertisement
Was this solution helpful?