-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestHappyPrefix.cpp
More file actions
30 lines (29 loc) · 875 Bytes
/
longestHappyPrefix.cpp
File metadata and controls
30 lines (29 loc) · 875 Bytes
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
// Source: https://leetcode.com/problems/longest-happy-prefix/
// Author: Miao Zhang
// Date: 2021-04-29
/***********************************************************
* character string hash
* p system:
* s = 'abc', a = 1, b = 2, c = 3
* hash(s) = a * p^2 + b * p^1 + c
************************************************************/
class Solution {
public:
string longestPrefix(string s) {
int kMod = 16769023;
int base = 128;
int n = s.length();
int p = 0;
int q = 0;
int coe = 1;
int res = 0;
for (int i = 1; i < n; i++) {
p = (p + coe * s[i - 1]) % kMod;
q = (q * base + s[n - i]) % kMod;
coe = (coe * base) % kMod;
if (p == q && s.substr(0, i) == s.substr(n - i))
res = i;
}
return string(s.substr(0, res));
}
};