forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
longest-happy-prefix.cpp
61 lines (56 loc) · 1.55 KB
/
longest-happy-prefix.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
// Time: O(n)
// Space: O(n)
// kmp solution
class Solution {
public:
string longestPrefix(string s) {
return s.substr(0, getPrefix(s).back() + 1);
}
private:
vector<int> 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;
}
};
// Time: O(n) on average
// Space: O(1)
// rolling-hash solution
class Solution2 {
public:
string longestPrefix(string s) {
static const int M = 1e9 + 7;
static const int D = 26;
int result = 0;
uint64_t prefix = 0, suffix = 0, power = 1;
for (int i = 0; i + 1 < s.length(); ++i) {
prefix = (prefix * D % M + (s[s.length() - (i + 1)] - 'a')) % M;
suffix = (suffix + (s[i] - 'a') * power % M) % M;
power = (power * D) % M;
if (prefix == suffix) {
// we assume M is a very large prime without hash collision
// assert(check(i + 1, s));
result = i + 1;
}
}
return s.substr(0, result);
}
private:
bool check(int l, const string& s) {
for (int i = 0; i < l; ++i) {
if (s[i] != s[s.length() - l + i]) {
return false;
}
}
return true;
}
};