Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6234437

Browse files
authored
feat: add solutions to lc problem: No.3008 (doocs#2239)
No.3008.Find Beautiful Indices in the Given Array II
1 parent 0bad77f commit 6234437

File tree

5 files changed

+723
-0
lines changed

5 files changed

+723
-0
lines changed

solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,164 @@
5757

5858
## 解法
5959

60+
### 方法一
61+
62+
<!-- tabs:start -->
63+
64+
```python
65+
class Solution:
66+
def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]:
67+
def build_prefix_function(pattern):
68+
prefix_function = [0] * len(pattern)
69+
j = 0
70+
for i in range(1, len(pattern)):
71+
while j > 0 and pattern[i] != pattern[j]:
72+
j = prefix_function[j - 1]
73+
if pattern[i] == pattern[j]:
74+
j += 1
75+
prefix_function[i] = j
76+
return prefix_function
77+
78+
def kmp_search(pattern, text, prefix_function):
79+
occurrences = []
80+
j = 0
81+
for i in range(len(text)):
82+
while j > 0 and text[i] != pattern[j]:
83+
j = prefix_function[j - 1]
84+
if text[i] == pattern[j]:
85+
j += 1
86+
if j == len(pattern):
87+
occurrences.append(i - j + 1)
88+
j = prefix_function[j - 1]
89+
return occurrences
90+
91+
prefix_a = build_prefix_function(a)
92+
prefix_b = build_prefix_function(b)
93+
94+
resa = kmp_search(a, s, prefix_a)
95+
resb = kmp_search(b, s, prefix_b)
96+
97+
res = []
98+
print(resa, resb)
99+
i = 0
100+
j = 0
101+
while i < len(resa):
102+
while j < len(resb):
103+
if abs(resb[j] - resa[i]) <= k:
104+
res.append(resa[i])
105+
break
106+
elif j + 1 < len(resb) and abs(resb[j + 1] - resa[i]) < abs(
107+
resb[j] - resa[i]
108+
):
109+
j += 1
110+
else:
111+
break
112+
i += 1
113+
return res
114+
```
115+
116+
```java
117+
public class Solution {
118+
public void computeLPS(String pattern, int[] lps) {
119+
int M = pattern.length();
120+
int len = 0;
121+
122+
lps[0] = 0;
123+
124+
int i = 1;
125+
while (i < M) {
126+
if (pattern.charAt(i) == pattern.charAt(len)) {
127+
len++;
128+
lps[i] = len;
129+
i++;
130+
} else {
131+
if (len != 0) {
132+
len = lps[len - 1];
133+
} else {
134+
lps[i] = 0;
135+
i++;
136+
}
137+
}
138+
}
139+
}
140+
141+
public List<Integer> KMP_codestorywithMIK(String pat, String txt) {
142+
int N = txt.length();
143+
int M = pat.length();
144+
List<Integer> result = new ArrayList<>();
145+
146+
int[] lps = new int[M];
147+
computeLPS(pat, lps);
148+
149+
int i = 0; // Index for text
150+
int j = 0; // Index for pattern
151+
152+
while (i < N) {
153+
if (pat.charAt(j) == txt.charAt(i)) {
154+
i++;
155+
j++;
156+
}
157+
158+
if (j == M) {
159+
result.add(i - j); // Pattern found at index i-j+1 (If you have to return 1 Based
160+
// indexing, that's why added + 1)
161+
j = lps[j - 1];
162+
} else if (i < N && pat.charAt(j) != txt.charAt(i)) {
163+
if (j != 0) {
164+
j = lps[j - 1];
165+
} else {
166+
i++;
167+
}
168+
}
169+
}
170+
171+
return result;
172+
}
173+
174+
private int lowerBound(List<Integer> list, int target) {
175+
int left = 0, right = list.size() - 1, result = list.size();
176+
177+
while (left <= right) {
178+
int mid = left + (right - left) / 2;
179+
180+
if (list.get(mid) >= target) {
181+
result = mid;
182+
right = mid - 1;
183+
} else {
184+
left = mid + 1;
185+
}
186+
}
187+
188+
return result;
189+
}
190+
191+
public List<Integer> beautifulIndices(String s, String a, String b, int k) {
192+
int n = s.length();
193+
194+
List<Integer> i_indices = KMP_codestorywithMIK(a, s);
195+
List<Integer> j_indices = KMP_codestorywithMIK(b, s);
196+
197+
List<Integer> result = new ArrayList<>();
198+
199+
for (int i : i_indices) {
200+
201+
int left_limit = Math.max(0, i - k); // To avoid out of bound -> I used max(0, i-k)
202+
int right_limit
203+
= Math.min(n - 1, i + k); // To avoid out of bound -> I used min(n-1, i+k)
204+
205+
int lowerBoundIndex = lowerBound(j_indices, left_limit);
206+
207+
if (lowerBoundIndex < j_indices.size()
208+
&& j_indices.get(lowerBoundIndex) <= right_limit) {
209+
result.add(i);
210+
}
211+
}
212+
213+
return result;
214+
}
215+
}
216+
```
217+
60218
```cpp
61219
class Solution {
62220
public:
@@ -125,4 +283,92 @@ private:
125283
};
126284
```
127285
286+
```go
287+
func beautifulIndices(s string, a string, b string, k int) []int {
288+
289+
s_len := len(s)
290+
a_len := len(a)
291+
b_len := len(b)
292+
293+
final := make([]int, 0)
294+
lps_a := make([]int, a_len)
295+
lps_b := make([]int, b_len)
296+
a_index := make([]int, 0)
297+
b_index := make([]int, 0)
298+
299+
var pat func(lps []int, s_l int, pattern string)
300+
301+
pat = func(lps []int, s_l int, pattern string) {
302+
303+
l := 0
304+
lps[0] = 0
305+
i := 1
306+
307+
for i < s_l {
308+
if pattern[i] == pattern[l] {
309+
l++
310+
lps[i] = l
311+
i++
312+
} else {
313+
if l != 0 {
314+
l = lps[l-1]
315+
} else {
316+
lps[i] = l
317+
i++
318+
}
319+
}
320+
}
321+
}
322+
323+
pat(lps_a, a_len, a)
324+
pat(lps_b, b_len, b)
325+
326+
var kmp func(pat string, pat_l int, lps []int, index *[]int)
327+
328+
kmp = func(pat string, pat_l int, lps []int, index *[]int) {
329+
i := 0
330+
j := 0
331+
for s_len-i >= pat_l-j {
332+
if s[i] == pat[j] {
333+
i++
334+
j++
335+
}
336+
if j == pat_l {
337+
*index = append(*index, i-pat_l)
338+
j = lps[j-1]
339+
} else if s[i] != pat[j] {
340+
if j != 0 {
341+
j = lps[j-1]
342+
} else {
343+
i++
344+
}
345+
}
346+
}
347+
}
348+
349+
kmp(a, a_len, lps_a, &a_index)
350+
kmp(b, b_len, lps_b, &b_index)
351+
352+
// fmt.Println(a_index, b_index)
353+
354+
i := 0
355+
j := 0
356+
357+
for i < len(a_index) && j < len(b_index) {
358+
if a_index[i]+k >= b_index[j] && a_index[i]-k <= b_index[j] {
359+
final = append(final, a_index[i])
360+
i++
361+
} else if a_index[i]-k > b_index[j] {
362+
j++
363+
} else {
364+
i++
365+
}
366+
}
367+
368+
return final
369+
}
370+
```
371+
372+
<!-- tabs:end -->
373+
128374
<!-- end -->

0 commit comments

Comments
 (0)