Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerEarth Substrings problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes