-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day-23
48 lines (47 loc) · 1.06 KB
/
Day-23
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
class Solution
{
public:
int maxFrequency(string s)
{
// code here
int n = s.size(), mn = n + 1, ans = 1, z[n] = {};
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (i <= r)z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]])z[i]++;
if (i + z[i] - 1 > r)l = i, r = i + z[i] - 1;
if (z[i] && z[i] + i == n)mn = min(mn, z[i]);
}
for (int i = 1; i < n; i++)if (z[i] >= mn)ans++;
return ans;
}
};
/*
int counter(string str,string s)
{
int m=s.length();
string st;
int ans=0;
for(int i=0;i<=str.length()-m;i++)
{
st=str.substr(i,m);
if(st==s)
ans++;
}
return ans;
}
int maxFrequency(string S)
{
// code here
string s1,s2;
for(int i=0,j=S.length()-1;i<j;i++,j--)
{
s1+=S[i];
s2=S[j]+s2;
if(s1==s2)
break;
}
if(s1!=s2||S.length()==1)
return 1;
return counter(S,s1);//counter count the frequency of substring s1 in string S
}
*/