forked from GeeksforGeeks-VIT-Bhopal/GeekWeek-Local
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecial String Again
More file actions
39 lines (31 loc) · 744 Bytes
/
Copy pathSpecial String Again
File metadata and controls
39 lines (31 loc) · 744 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
31
32
33
34
35
36
37
38
39
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,c,i=0,j,ans=0;
cin>>n;
string s;
cin>>s;
int same_char_count[n]={0};
while(i<n) {
j=i+1,c=1;
while(j<n && s[i]==s[j]) {
++j,++c;
}
//total substrings which have all same char(s)
ans+=(c*(c+1))>>1;
same_char_count[i]=c;
i=j;
}
for(j=1;j<n-1;++j) {
if(s[j]==s[j-1]) {
same_char_count[j] = same_char_count[j-1];
}
//odd length substr(s) which has middle element diiferent
if(s[j-1]==s[j+1] && s[j]!=s[j-1]) {
ans += min(same_char_count[j-1], same_char_count[j+1]);
}
}
cout<<ans<<endl;
return 0;
}