Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
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 <= 1001 <= patterns[i].length <= 1001 <= word.length <= 100patterns[i] and word consist of lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
["a","abc","bc","d"] "abc"
["a","b","c"] "aaaaabbbbb"
["a","a","a"] "ab"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1967: Number of Strings That Appear as Substrings in Word
class Solution {
public int numOfStrings(String[] patterns, String word) {
int ans = 0;
for (String p : patterns) {
if (word.contains(p)) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1967: Number of Strings That Appear as Substrings in Word
func numOfStrings(patterns []string, word string) (ans int) {
for _, p := range patterns {
if strings.Contains(word, p) {
ans++
}
}
return
}
# Accepted solution for LeetCode #1967: Number of Strings That Appear as Substrings in Word
class Solution:
def numOfStrings(self, patterns: List[str], word: str) -> int:
return sum(p in word for p in patterns)
// Accepted solution for LeetCode #1967: Number of Strings That Appear as Substrings in Word
impl Solution {
pub fn num_of_strings(patterns: Vec<String>, word: String) -> i32 {
patterns.iter().filter(|p| word.contains(&**p)).count() as i32
}
}
// Accepted solution for LeetCode #1967: Number of Strings That Appear as Substrings in Word
function numOfStrings(patterns: string[], word: string): number {
return patterns.filter(p => word.includes(p)).length;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.