Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Substrings problem solution

YASH PAL, 31 July 202417 February 2026
In this HackerEarth Substrings problem solution You have a string S, but you like only special strings. So, you have to calculate the total number of special substrings in S.
 
A string T, of length L, is called special string, if either of the following property holds:
  1. All characters of the string T are same. for example, aaa
  2. The string has an odd length (i.e, L is odd) and all characters of T are same except the middle character, for example, aabaa.
Count the total number of special substrings in S.
 
 
HackerEarth Substrings problem solution

 

 

HackerEarth Substrings problem solution.

#include<bits/stdc++.h>
using namespace std;

//Constants:
#define N 1000005
#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pritnf printf

ll le[N];
ll ri[N];

int main()
{
ll t,i=0,j,k,x,y,z,count=0,p,flag=0,ans=0,sum=0,l,n,m,max1,min1,pos,tmp,q;

//string s;

t=1;

while(t--)
{
string s;
cin >> s;
l=s.length();

ans=0;
//condition 1
count=1;
for(i=1;i<l;i++)
{
if(s[i]==s[i-1])
count++;
else
{
ans+=count*(count+1)/2;
count=1;
}
}
ans+=count*(count+1)/2;
//printf("%lld ",ans);

//condition 2
count=1;
for(i=1;i<l;i++)
{
if(s[i]==s[i-1])
count++;
else
{
le[i]=count;
count=1;
}
}

count=1;
for(i=l-2;i>=0;i--)
{
if(s[i]==s[i+1])
count++;
else
{
ri[i]=count;
count=1;
}
}

for(i=1;i<l-1;i++)
{
p=min(le[i],ri[i]);

if(s[i-1]==s[i+1] && s[i]!=s[i+1])
ans+=p;
//printf("%lld ",ans);
}

printf("%lldn",ans);

}


return 0;
}
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes