-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3008-find-beautiful-indices-in-the-given-array-ii.cpp
More file actions
67 lines (63 loc) · 1.81 KB
/
Copy path3008-find-beautiful-indices-in-the-given-array-ii.cpp
File metadata and controls
67 lines (63 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Solution {
public:
vector<int> computeLPS(string& pattern) {
int m = (int) pattern.size();
vector<int>lps = vector<int>(m);
int len = 0;
lps[0] = 0;
int index = 1;
while (index < m) {
if (pattern[index] == pattern[len]) {
len++;
lps[index] = len;
index++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[index] = 0;
index++;
}
}
}
return lps;
}
vector<int> searchPattern(string& txt, string& pattern, vector<int>& lps) {
int m = (int) pattern.size();
int n = (int) txt.size();
vector<int> res;
int i = 0; // index for txt
int j = 0; // index for pattern
while (i < n) {
if (pattern[j] == txt[i]) {
j++;
i++;
}
if (j == m) {
res.push_back(i - j);
j = lps[j - 1];
} else if (i < n && pattern[j] != txt[i]) {
if (j != 0) j = lps[j - 1];
else i = i + 1;
}
}
return res;
}
vector<int> beautifulIndices(string s, string a, string b, int k) {
vector<int> alps = computeLPS(a);
vector<int> blps = computeLPS(b);
vector<int> as = searchPattern(s, a, alps);
vector<int> bs = searchPattern(s, b, blps);
vector<int> ans;
int j = 0;
for (auto &idx: as) {
while (j < bs.size() && idx - bs[j] > k) {
j++;
}
if (j < bs.size() && abs(bs[j] - idx) <= k) {
ans.push_back(idx);
}
};
return ans;
}
};