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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed integer array nums of size n, and a 0-indexed integer array pattern of size m consisting of integers -1, 0, and 1.
A subarray nums[i..j] of size m + 1 is said to match the pattern if the following conditions hold for each element pattern[k]:
nums[i + k + 1] > nums[i + k] if pattern[k] == 1.nums[i + k + 1] == nums[i + k] if pattern[k] == 0.nums[i + k + 1] < nums[i + k] if pattern[k] == -1.Return the count of subarrays in nums that match the pattern.
Example 1:
Input: nums = [1,2,3,4,5,6], pattern = [1,1] Output: 4 Explanation: The pattern [1,1] indicates that we are looking for strictly increasing subarrays of size 3. In the array nums, the subarrays [1,2,3], [2,3,4], [3,4,5], and [4,5,6] match this pattern. Hence, there are 4 subarrays in nums that match the pattern.
Example 2:
Input: nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1] Output: 2 Explanation: Here, the pattern [1,0,-1] indicates that we are looking for a sequence where the first number is smaller than the second, the second is equal to the third, and the third is greater than the fourth. In the array nums, the subarrays [1,4,4,1], and [3,5,5,3] match this pattern. Hence, there are 2 subarrays in nums that match the pattern.
Constraints:
2 <= n == nums.length <= 1001 <= nums[i] <= 1091 <= m == pattern.length < n-1 <= pattern[i] <= 1Problem summary: You are given a 0-indexed integer array nums of size n, and a 0-indexed integer array pattern of size m consisting of integers -1, 0, and 1. A subarray nums[i..j] of size m + 1 is said to match the pattern if the following conditions hold for each element pattern[k]: nums[i + k + 1] > nums[i + k] if pattern[k] == 1. nums[i + k + 1] == nums[i + k] if pattern[k] == 0. nums[i + k + 1] < nums[i + k] if pattern[k] == -1. Return the count of subarrays in nums that match the pattern.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · String Matching
[1,2,3,4,5,6] [1,1]
[1,4,4,1,3,5,5,3] [1,0,-1]
count-the-number-of-incremovable-subarrays-i)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3034: Number of Subarrays That Match a Pattern I
class Solution {
public int countMatchingSubarrays(int[] nums, int[] pattern) {
int n = nums.length, m = pattern.length;
int ans = 0;
for (int i = 0; i < n - m; ++i) {
int ok = 1;
for (int k = 0; k < m && ok == 1; ++k) {
if (f(nums[i + k], nums[i + k + 1]) != pattern[k]) {
ok = 0;
}
}
ans += ok;
}
return ans;
}
private int f(int a, int b) {
return a == b ? 0 : (a < b ? 1 : -1);
}
}
// Accepted solution for LeetCode #3034: Number of Subarrays That Match a Pattern I
func countMatchingSubarrays(nums []int, pattern []int) (ans int) {
f := func(a, b int) int {
if a == b {
return 0
}
if a < b {
return 1
}
return -1
}
n, m := len(nums), len(pattern)
for i := 0; i < n-m; i++ {
ok := 1
for k := 0; k < m && ok == 1; k++ {
if f(nums[i+k], nums[i+k+1]) != pattern[k] {
ok = 0
}
}
ans += ok
}
return
}
# Accepted solution for LeetCode #3034: Number of Subarrays That Match a Pattern I
class Solution:
def countMatchingSubarrays(self, nums: List[int], pattern: List[int]) -> int:
def f(a: int, b: int) -> int:
return 0 if a == b else (1 if a < b else -1)
ans = 0
for i in range(len(nums) - len(pattern)):
ans += all(
f(nums[i + k], nums[i + k + 1]) == p for k, p in enumerate(pattern)
)
return ans
// Accepted solution for LeetCode #3034: Number of Subarrays That Match a Pattern I
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3034: Number of Subarrays That Match a Pattern I
// class Solution {
// public int countMatchingSubarrays(int[] nums, int[] pattern) {
// int n = nums.length, m = pattern.length;
// int ans = 0;
// for (int i = 0; i < n - m; ++i) {
// int ok = 1;
// for (int k = 0; k < m && ok == 1; ++k) {
// if (f(nums[i + k], nums[i + k + 1]) != pattern[k]) {
// ok = 0;
// }
// }
// ans += ok;
// }
// return ans;
// }
//
// private int f(int a, int b) {
// return a == b ? 0 : (a < b ? 1 : -1);
// }
// }
// Accepted solution for LeetCode #3034: Number of Subarrays That Match a Pattern I
function countMatchingSubarrays(nums: number[], pattern: number[]): number {
const f = (a: number, b: number) => (a === b ? 0 : a < b ? 1 : -1);
const n = nums.length;
const m = pattern.length;
let ans = 0;
for (let i = 0; i < n - m; ++i) {
let ok = 1;
for (let k = 0; k < m && ok; ++k) {
if (f(nums[i + k], nums[i + k + 1]) !== pattern[k]) {
ok = 0;
}
}
ans += ok;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
At each of the n starting positions in the text, compare up to m characters with the pattern. If a mismatch occurs, shift by one and restart. Worst case (e.g., searching "aab" in "aaaa...a") checks m characters at nearly every position: O(n × m).
KMP and Z-algorithm preprocess the pattern in O(m) to build a failure/Z-array, then scan the text in O(n) — never backtracking. Total: O(n + m). Rabin-Karp uses rolling hashes for O(n + m) expected time. All beat the O(n × m) brute force of checking every position.
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.