forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-beautiful-indices-in-the-given-array-i.cpp
104 lines (98 loc) · 3.49 KB
/
find-beautiful-indices-in-the-given-array-i.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Time: O(n), x = len(KMP(s, a)), y = len(KMP(s, b))
// Space: O(min(a + b + x + y, n))
// kmp, two pointers
class Solution {
public:
vector<int> beautifulIndices(string s, string a, string b, int k) {
const auto& KMP = [&](const string& text, const string& pattern) {
const auto& getPrefix = [&](const string& pattern) {
vector<int> prefix(pattern.length(), -1);
int j = -1;
for (int i = 1; i < pattern.length(); ++i) {
while (j > -1 && pattern[j + 1] != pattern[i]) {
j = prefix[j];
}
if (pattern[j + 1] == pattern[i]) {
++j;
}
prefix[i] = j;
}
return prefix;
};
vector<int> result;
const vector<int> prefix = getPrefix(pattern);
int j = -1;
for (int i = 0; i < text.length(); ++i) {
while (j > -1 && pattern[j + 1] != text[i]) {
j = prefix[j];
}
if (pattern[j + 1] == text[i]) {
++j;
}
if (j == pattern.length() - 1) {
result.emplace_back(i - j);
j = prefix[j];
}
}
return result;
};
vector<int> result;
if (!(size(a) <= size(s) && size(b) <= size(s))) {
return result;
}
const auto& lookup = KMP(s, b);
int j = 0;
for (const auto& i : KMP(s, a)) {
for (; j < size(lookup) && lookup[j] < i - k; ++j);
if (j < size(lookup) && lookup[j] <= i + k) {
result.emplace_back(i);
}
}
return result;
}
};
// Time: O(n + xlogy), x = len(KMP(s, a)), y = len(KMP(s, b))
// Space: O(n)
// kmp, binary search
class Solution2 {
public:
vector<int> beautifulIndices(string s, string a, string b, int k) {
const auto& KMP = [&](const string& text, const string& pattern) {
const auto& getPrefix = [&](const string& pattern) {
vector<int> prefix(pattern.length(), -1);
int j = -1;
for (int i = 1; i < pattern.length(); ++i) {
while (j > -1 && pattern[j + 1] != pattern[i]) {
j = prefix[j];
}
if (pattern[j + 1] == pattern[i]) {
++j;
}
prefix[i] = j;
}
return prefix;
};
vector<int> result;
const vector<int> prefix = getPrefix(pattern + '#' + text);
for (int i = (size(pattern) + 1) + (size(pattern) - 1); i < size(prefix); ++i) {
if (prefix[i] + 1 == size(pattern)) {
result.emplace_back((i - (size(pattern) + 1)) - (size(pattern) - 1));
}
}
return result;
};
vector<int> result;
if (!(size(a) <= size(s) && size(b) <= size(s))) {
return result;
}
const auto& lookup = KMP(s, b);
int j = 0;
for (const auto& i : KMP(s, a)) {
const int j = distance(cbegin(lookup), lower_bound(cbegin(lookup), cend(lookup), i - k));
if (j < size(lookup) && lookup[j] <= i + k) {
result.emplace_back(i);
}
}
return result;
}
};