Skip to content

Commit aafaa85

Browse files
authored
Update shortest-matching-substring.cpp
1 parent 4fb4e44 commit aafaa85

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

C++/shortest-matching-substring.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,78 @@ class Solution {
77
int shortestMatchingSubstring(string s, string p) {
88
static const int INF = numeric_limits<int>::max();
99

10+
const auto& KMP = [](const auto& text, const auto& pattern) {
11+
const auto& getPrefix = [](const auto& pattern) {
12+
vector<int> prefix(size(pattern), -1);
13+
int j = -1;
14+
for (int i = 1; i < size(pattern); ++i) {
15+
while (j != -1 && pattern[j + 1] != pattern[i]) {
16+
j = prefix[j];
17+
}
18+
if (pattern[j + 1] == pattern[i]) {
19+
++j;
20+
}
21+
prefix[i] = j;
22+
}
23+
return prefix;
24+
};
25+
26+
vector<int> result;
27+
if (empty(pattern)) {
28+
result.resize(size(text) + 1);
29+
iota(begin(result), end(result), 0);
30+
return result;
31+
}
32+
const vector<int> prefix = getPrefix(pattern);
33+
int j = -1;
34+
for (int i = 0; i < size(text); ++i) {
35+
while (j > -1 && pattern[j + 1] != text[i]) {
36+
j = prefix[j];
37+
}
38+
if (pattern[j + 1] == text[i]) {
39+
++j;
40+
}
41+
if (j == size(pattern) - 1) {
42+
result.emplace_back(i - j);
43+
j = prefix[j];
44+
}
45+
}
46+
return result;
47+
};
48+
49+
const int i = p.find('*');
50+
const auto& a = p.substr(0, i);
51+
const int j = p.find('*', i + 1);
52+
const auto& b = p.substr(i + 1, j - (i + 1));
53+
const auto& c = p.substr(j + 1);
54+
const int n = size(s), la = size(a), lb = size(b), lc = size(c);
55+
int result = INF;
56+
const auto& idxs1 = KMP(s, b);
57+
const auto& idxs2 = KMP(s, c);
58+
int x = 0, y = 0;
59+
for (const auto& i : KMP(s, a)) {
60+
for (; x < size(idxs1) && idxs1[x] < i + la; ++x);
61+
if (x == size(idxs1)) {
62+
break;
63+
}
64+
for (; y < size(idxs2) && idxs2[y] < idxs1[x] + lb; ++y);
65+
if (y == size(idxs2)) {
66+
break;
67+
}
68+
result = min(result, (idxs2[y] + lc) - i);
69+
}
70+
return result != INF ? result : -1;
71+
}
72+
};
73+
74+
// Time: O(n + m)
75+
// Space: O(n + m)
76+
// kmp, two pointers (three pointers)
77+
class Solution2 {
78+
public:
79+
int shortestMatchingSubstring(string s, string p) {
80+
static const int INF = numeric_limits<int>::max();
81+
1082
const auto& getPrefix = [](const string& pattern) {
1183
vector<int> prefix(size(pattern), -1);
1284
int j = -1;

0 commit comments

Comments
 (0)