STRCH - Friend or Girlfriend Solution Video

cpp
easy
video
april19

#1

A solution video for Problem STRCH - Friend or Girlfriend


#2

Hey I don’t know why my code didn’t work for problem ? Could you please help me !!
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t–)
{
int n;
cin >> n;
string s; char a;
cin >> s >> a;
vector v;
for(int i=0;i<n;i++)
{
if(s[i]==a) v.push_back(i);
}
if(v.size()==0)
{
cout << 0 << ‘\n’; continue;
}
int sum = 0,j=0;
for(int i=0;i<n;i++)
{
if(i<=v[j])
{
sum += n-v[j];
// cout << “If sum :” << sum << " " << i<< ‘\n’;
}
else
{
j++;
sum += n-v[j];
// cout << "ELse sum : " << sum << ‘\n’;
}
}
cout << sum << ‘\n’;

}

}


#3

@betuyash - I saw your accepted solution for this problem. Please find my solution link below. This solution follows a different approach. Hope it would be informative.

https://www.codechef.com/viewsolution/23911106


#4

Thanks for sharing your approach !
But I don’t know why My above code failed … It is giving same output in many test cases as that an accepted solution give ?