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 The Alphabet Chocolate problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth The Alphabet Chocolate problem solution A long chocolate was given as a gift to Alice by Bob. The chocolate is a series of square tiles connected to each other in a straight line. Each chocolate piece contains an alphabet carved on it.
 
Now Alice picks a single part of chocolate and eats it. The taste of a chocolate’s part is determined by how many squares are there in that part on which a vowel is carved.
 
Since there are lots of ways for Alice to choose her chocolate part, find the sum of tastes of all the possible chocolate parts that can be cut out of the given chocolate.
 
 
HackerEarth The Alphabet Chocolate problem solution

 

 

HackerEarth The Alphabet Chocolate problem solution.

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define si(x) scanf("%d",&x)
#define pi(x) printf("%dn",x)
#define s(x) scanf("%lld",&x)
#define p(x) printf("%lldn",x)
#define sc(x) scanf("%s",x)
#define pc(x) printf("%s",x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define F first
#define S second
#define inf 1e18
#define prec(x) fixed<<setprecision(15)<<x
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define mem(x,y) memset(x,y,sizeof(x))
#define PQG priority_queue< int,std::vector<int>,std::greater<int> >
#define PQL priority_queue< int,std::vector<int>,std::less<int> >
#define PQPL priority_queue<pii ,vector< pii >, less< pii > >
#define PQPG priority_queue<pii ,vector< pii >, greater< pii > >
#define PQPGB priority_queue<pii ,vector< pll >, greater< pll > >
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)

using namespace std;

bool isvowel(char c) {
return (c=='a' || c=='e' || c=='i' || c=='o' || c=='u'
|| c=='A' || c=='E' || c=='I' || c=='O' || c=='U');
}

int main() {
#ifndef ONLINE_JUDGE
freopen("inp.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
fast_io;
int t; cin>>t;
assert(t>=1 && t<=10);
while(t--) {
string s; cin>>s;
int n=s.size();
assert(n>=1 && n<=100000);
for(int i=0;i<n;i++) {
assert((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z'));
}
ll ans=0;
for(int i=0;i<n;i++) {
ll val=1LL*(i+1)*(n-i);
if(isvowel(s[i])) ans+=val;
}
cout<<ans<<endl;
}

return 0;
}
 
 

Second solution

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

int main() {
int t; cin>>t;
while(t--){
string s;
cin>>s;
int arr[s.length()+1];
memset(arr,0,sizeof(arr));
for(int i=0;i<s.length();i++){
if(s[i]=='a' || s[i]=='e' || s[i]=='o' || s[i]=='i' || s[i]=='u') arr[i]=1;
else if(s[i]=='A' || s[i]=='E' || s[i]=='O' || s[i]=='I' || s[i]=='U') arr[i]=1;
}
long long sum=0;
for(int i=0;i<s.length();i++){
if(arr[i]==1) sum+=((s.length()-i)*(i+1));
}
cout<<sum<<"n";

}
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