-
Notifications
You must be signed in to change notification settings - Fork 0
/
GFG Sum of Beauty of All Substrings
52 lines (51 loc) · 1.17 KB
/
GFG Sum of Beauty of All Substrings
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
class Solution {
public:
int beautySum(string s) {
int n=s.size();int ans=0;
for(int l=2;l<=n;l++)
{
int i=0;
int j=0;
vector<int> freq(26,0);
while(j<n)
{
freq[s[j]-'a']++;
if((j-i+1)==l)
{
//cout<<i<<" to "<<j<<" ";
//cout<<max_element(freq)-min_element(freq)<<endl;
ans=ans+(max_element(freq)-min_element(freq));
freq[s[i]-'a']--;
++i;
}
++j;
}
}
//cout<<"Answer"<<endl;
return ans;
}
int max_element(vector<int> &freq)
{
int maxi=0;
for(int i=0;i<26;i++)
{
if(freq[i]!=0)
{
maxi=max(maxi,freq[i]);
}
}
return maxi;
}
int min_element(vector<int> &freq)
{
int mini=INT_MAX;
for(int i=0;i<26;i++)
{
if(freq[i]!=0)
{
mini=min(mini,freq[i]);
}
}
return mini;
}
};