-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
shortest-string-that-contains-three-strings.cpp
94 lines (88 loc) · 2.97 KB
/
shortest-string-that-contains-three-strings.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
// Time: O(l)
// Space: O(l)
// brute force, longest prefix suffix, kmp algorithm
class Solution {
public:
string minimumString(string a, string b, string c) {
const auto& getPrefix = [](const string& pattern) {
vector<int> prefix(size(pattern), -1);
int j = -1;
for (int i = 1; i < size(pattern); ++i) {
while (j != -1 && pattern[j + 1] != pattern[i]) {
j = prefix[j];
}
if (pattern[j + 1] == pattern[i]) {
++j;
}
prefix[i] = j;
}
return prefix;
};
const auto& KMP = [&](const string& text, const string& pattern) {
const vector<int> prefix = getPrefix(pattern);
int j = -1;
for (int i = 0; i < size(text); ++i) {
while (j > -1 && pattern[j + 1] != text[i]) {
j = prefix[j];
}
if (pattern[j + 1] == text[i]) {
++j;
}
if (j == size(pattern) - 1) {
return i - j;
}
}
return -1;
};
const auto& merge = [&](const auto& a, const auto& b) {
if (KMP(b, a) != -1) {
return b;
}
const auto& prefix = getPrefix(b + "#" + a);
const auto& l = prefix.back() + 1; // longest prefix suffix length
return a + b.substr(l);
};
vector<string> result = {
merge(a, merge(b, c)), merge(a, merge(c, b)),
merge(b, merge(a, c)), merge(b, merge(c, a)),
merge(c, merge(a, b)), merge(c, merge(b, a))
};
return *min_element(cbegin(result), cend(result), [](const auto& a, const auto& b) {
return size(a) != size(b) ? size(a) < size(b) : a < b;
});
}
};
// Time: O(l^2)
// Space: O(l)
// brute force
class Solution2 {
public:
string minimumString(string a, string b, string c) {
const auto& merge = [](const auto& a, const auto& b) {
if (b.find(a) != string::npos) {
return b;
}
int l = min(size(a), size(b));
for (; l >= 0; --l) {
int i = (size(a) - l), j = 0;
for (; j < l; ++i, ++j) {
if (a[i] != b[j]) {
break;
}
}
if (j == l) {
break;
}
}
return a + b.substr(l);
};
vector<string> result = {
merge(a, merge(b, c)), merge(a, merge(c, b)),
merge(b, merge(a, c)), merge(b, merge(c, a)),
merge(c, merge(a, b)), merge(c, merge(b, a))
};
return *min_element(cbegin(result), cend(result), [](const auto& a, const auto& b) {
return size(a) != size(b) ? size(a) < size(b) : a < b;
});
}
};