-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path1392. Longest Happy Prefix.cpp
158 lines (131 loc) · 4.64 KB
/
1392. 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//either Time Limit Exceeded or Memory Limit Exceeded
//69 / 72 test cases passed.
class Solution {
public:
string longestPrefix(string s) {
int N = s.size();
int SEGMENT = 10000;
//l starts from N-1(substring cannot be itself)
for(int l = N-1; l >= 1; l--){
// cout << l << endl;
// string prefix = s.substr(0, l);
// string suffix = s.substr(N-l);
// bool same = true;
// for(int i = 0; i < prefix.size(); i++){
// if(prefix[i] != suffix[i]){
// same = false;
// break;
// }
// }
// if(same){
// return prefix;
// }
// if(prefix == suffix){
// return prefix;
// }
bool same = true;
for(int i = 0; i <= l/SEGMENT; i++){
//partPrefix could exceed the boundary
string partPrefix = s.substr(0+i*SEGMENT, SEGMENT);
string partSuffix = s.substr(N-l+i*SEGMENT, SEGMENT);
//partPrefix could exceed the boundary
if(partPrefix.size() != partSuffix.size()){
partPrefix = partPrefix.substr(0, partSuffix.size());
}
for(int i = 0; i < partPrefix.size(); i++){
if(partPrefix[i] != partSuffix[i]){
same = false;
break;
}
}
if(same == false){
break;
}
// if(partPrefix != partSuffix){
// same = false;
// break;
// }
}
if(same){
return s.substr(0, l);
}
}
return "";
}
};
//KMP
//Runtime: 48 ms, faster than 28.57% of C++ online submissions for Longest Happy Prefix.
//Memory Usage: 19 MB, less than 100.00% of C++ online submissions for Longest Happy Prefix.
class Solution {
public:
vector<int> preprocess(string& pattern){
int n = pattern.size();
vector<int> lps(n, 0);
for(int i = 1, len = 0; i < n; ){
if(pattern[i] == pattern[len]){
len++;
lps[i] = len;
i++;
}else if(len > 0){
len = lps[len-1];
}else{
i++;
}
}
return lps;
};
string longestPrefix(string s) {
vector<int> lps = preprocess(s);
// for(int e : lps){
// cout << e;
// }
// cout << endl;
// int length = *max_element(lps.begin(), lps.end());
int length = lps[lps.size()-1];
return s.substr(0, length);
}
};
//Rolling hash
//https://leetcode.com/problems/longest-happy-prefix/discuss/547448/Python-rolling-hash
//Runtime: 296 ms, faster than 14.29% of C++ online submissions for Longest Happy Prefix.
//Memory Usage: 14.3 MB, less than 100.00% of C++ online submissions for Longest Happy Prefix.
class Solution {
public:
//https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/
long long int power(long long int x, long long int y, long long int p){
long long int res = 1;
// Update x if it is more than or equal to p
x = x % p;
while(y > 0){
// If y is odd, multiply x with result
if(y & 1){
res = (res*x) % p;
}
// y must be even now
y >>= 1;
x = (x*x) % p;
}
return res;
};
string longestPrefix(string s) {
int N = s.size();
if(N == 1) return "";
long long int mod = pow(10, 9) + 7;
long long int prefixHash = 0, suffixHash = 0;
int res = -1;
//i cannot be N-1 because we only want the proper substrings
for(int i = 0; i < N-1; i++){
// prefixHash += (s[i]-'a') * ((long long int)pow(26, i) % mod);
prefixHash += (s[i]-'a') * power(26, i, mod);
prefixHash %= mod;
//Note it's s[N-1-i] here!
suffixHash = suffixHash * 26 + (s[N-1-i] - 'a');
suffixHash %= mod;
// cout << i << " " << prefixHash << " " << suffixHash << endl;
if(prefixHash == suffixHash){
res = i;
}
}
return (res == -1) ? "" : s.substr(0, res+1);
}
};