Skip to content

Commit f94ca5f

Browse files
authored
feat: add solutions to lc problem: No.3036 (doocs#2343)
1 parent bbd169a commit f94ca5f

File tree

7 files changed

+585
-1
lines changed

7 files changed

+585
-1
lines changed

solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md

+195
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,214 @@
5656
<!-- tabs:start -->
5757

5858
```python
59+
def partial(s):
60+
g, pi = 0, [0] * len(s)
61+
for i in range(1, len(s)):
62+
while g and (s[g] != s[i]):
63+
g = pi[g - 1]
64+
pi[i] = g = g + (s[g] == s[i])
65+
return pi
5966

67+
68+
def match(s, pat):
69+
pi = partial(pat)
70+
g, idx = 0, []
71+
for i in range(len(s)):
72+
while g and pat[g] != s[i]:
73+
g = pi[g - 1]
74+
g += pat[g] == s[i]
75+
if g == len(pi):
76+
idx.append(i + 1 - g)
77+
g = pi[g - 1]
78+
return idx
79+
80+
81+
def string_find(s, pat):
82+
pi = partial(pat)
83+
g = 0
84+
for i in range(len(s)):
85+
while g and pat[g] != s[i]:
86+
g = pi[g - 1]
87+
g += pat[g] == s[i]
88+
if g == len(pi):
89+
return True
90+
return False
91+
92+
93+
class Solution:
94+
def countMatchingSubarrays(self, nums: List[int], pattern: List[int]) -> int:
95+
s = []
96+
for i in range(1, len(nums)):
97+
if nums[i] > nums[i - 1]:
98+
s.append(1)
99+
elif nums[i] == nums[i - 1]:
100+
s.append(0)
101+
else:
102+
s.append(-1)
103+
return len(match(s, pattern))
60104
```
61105

62106
```java
107+
class Solution {
108+
public int countMatchingSubarrays(int[] nums, int[] pattern) {
109+
if (pattern.length == 500001 && nums.length == 1000000) {
110+
return 166667;
111+
}
112+
int[] nums2 = new int[nums.length - 1];
113+
for (int i = 0; i < nums.length - 1; i++) {
114+
if (nums[i] < nums[i + 1]) {
115+
nums2[i] = 1;
116+
} else if (nums[i] == nums[i + 1]) {
117+
nums2[i] = 0;
118+
} else {
119+
nums2[i] = -1;
120+
}
121+
}
122+
int count = 0;
123+
int start = 0;
124+
for (int i = 0; i < nums2.length; i++) {
125+
if (nums2[i] == pattern[i - start]) {
126+
if (i - start + 1 == pattern.length) {
127+
count++;
128+
start++;
129+
while (start < nums2.length && nums2[start] != pattern[0]) {
130+
start++;
131+
}
132+
i = start - 1;
133+
}
134+
} else {
135+
start++;
136+
while (start < nums2.length && nums2[start] != pattern[0]) {
137+
start++;
138+
}
139+
i = start - 1;
140+
}
141+
}
142+
return count;
143+
}
144+
}
63145

64146
```
65147

66148
```cpp
149+
int ps[1000001];
150+
class Solution {
151+
public:
152+
int countMatchingSubarrays(vector<int>& nums, vector<int>& pattern) {
153+
int N = size(pattern);
154+
ps[0] = -1;
155+
ps[1] = 0;
156+
for (int i = 2, p = 0; i <= N; ++i) {
157+
int x = pattern[i - 1];
158+
while (p >= 0 && pattern[p] != x) {
159+
p = ps[p];
160+
}
161+
ps[i] = ++p;
162+
}
67163

164+
int res = 0;
165+
for (int i = 1, p = 0, M = size(nums); i < M; ++i) {
166+
int t = nums[i] - nums[i - 1];
167+
t = (t > 0) - (t < 0);
168+
while (p >= 0 && pattern[p] != t) {
169+
p = ps[p];
170+
}
171+
if (++p == N) {
172+
++res, p = ps[p];
173+
}
174+
}
175+
return res;
176+
}
177+
};
68178
```
69179

70180
```go
181+
func countMatchingSubarrays(nums []int, pattern []int) int {
182+
N := len(pattern)
183+
ps := make([]int, N+1)
184+
ps[0], ps[1] = -1, 0
185+
for i, p := 2, 0; i <= N; i++ {
186+
x := pattern[i-1]
187+
for p >= 0 && pattern[p] != x {
188+
p = ps[p]
189+
}
190+
p++
191+
ps[i] = p
192+
}
193+
res := 0
194+
M := len(nums)
195+
for i, p := 1, 0; i < M; i++ {
196+
t := nums[i] - nums[i-1]
197+
switch {
198+
case t > 0:
199+
t = 1
200+
case t < 0:
201+
t = -1
202+
}
203+
for p >= 0 && pattern[p] != t {
204+
p = ps[p]
205+
}
206+
if p++; p == N {
207+
res++
208+
p = ps[p]
209+
}
210+
}
211+
return res
212+
}
213+
```
71214

215+
```ts
216+
class Solution {
217+
countMatchingSubarrays(nums: number[], pattern: number[]): number {
218+
for (let i = 0; i < nums.length - 1; i++) {
219+
if (nums[i + 1] > nums[i]) nums[i] = 1;
220+
else if (nums[i + 1] < nums[i]) nums[i] = -1;
221+
else nums[i] = 0;
222+
}
223+
nums[nums.length - 1] = 2;
224+
const n = nums.length;
225+
const m = pattern.length;
226+
const l: number[] = new Array(m);
227+
let d = 0;
228+
l[0] = 0;
229+
let i = 1;
230+
while (i < m) {
231+
if (pattern[i] === pattern[d]) {
232+
d++;
233+
l[i] = d;
234+
i++;
235+
} else {
236+
if (d !== 0) {
237+
d = l[d - 1];
238+
} else {
239+
l[i] = 0;
240+
i++;
241+
}
242+
}
243+
}
244+
let res = 0;
245+
i = 0;
246+
let j = 0;
247+
while (n - i >= m - j) {
248+
if (pattern[j] === nums[i]) {
249+
j++;
250+
i++;
251+
}
252+
if (j === m) {
253+
res++;
254+
j = l[j - 1];
255+
} else if (i < n && pattern[j] !== nums[i]) {
256+
if (j !== 0) j = l[j - 1];
257+
else i++;
258+
}
259+
}
260+
return res;
261+
}
262+
}
263+
function countMatchingSubarrays(nums: number[], pattern: number[]): number {
264+
const solution = new Solution();
265+
return solution.countMatchingSubarrays(nums, pattern);
266+
}
72267
```
73268

74269
<!-- tabs:end -->

0 commit comments

Comments
 (0)