-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path796.cpp
More file actions
56 lines (47 loc) · 1.62 KB
/
796.cpp
File metadata and controls
56 lines (47 loc) · 1.62 KB
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
class Solution {
public:
int pow(int p, int n, int M){ //p^n mod M
if(n == 0) return 1;
else if(n == 1) return p%M;
else if(n % 2 == 0){
int sq = pow(p, n/2, M);
return (int) ((long long) sq * sq % M);
}
return ((long long) p*pow(p, n-1, M))%M;
}
const int M = 1e9 + 7;
const int p = 113;
bool rotateString(string s, string goal) { //O(N) using hashing
if(s.length() != goal.length()) return false;
long long PtoN = pow(p, goal.length(), M);
long long hash = 0;
long long g = 0;
for(int i = 0; i < s.length(); i++){
hash = (hash*p) % M; hash = (hash+(s[i]-'a')) % M;
g = (g*p) % M; g = (g+(goal[i]-'a')) % M;
}
if(g == hash) return true;
for(char x : s){
hash = (hash*p) % M;
hash = (hash - (PtoN*(x-'a') % M)) % M;
if(hash < 0) hash = (hash + M) % M;
hash = (hash + (x-'a')) % M;
if(g == hash) return true;
}
return false;
}
bool rotateStringKMP(string s, string goal) { //O(N) with KMP
if(s.length() != goal.length()) return false;
s = goal + "#" + s + s.substr(0, s.length()-1);
vector<int> lps(s.length());
for(int i = 1; i < s.length(); i++){
int j = lps[i-1];
while(j > 0 && s[i] != s[j]){
j = lps[j-1];
}
if(s[i] == s[j]) lps[i] = j+1;
if(lps[i] == goal.length()) return true;
}
return false;
}
};