My issue
I tried the classical approach for finding atleast 1 i.e total substrings - substrings containing 0 required char. dont know why it is failing in some cases
My code
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin >> t;
for(int i = 0; i < t; i++)
{
int n;
cin >> n;
string test;
char c;
cin >> test >> c;
// atleast 1 means total substrings - substring with 0 such char
// nnncnnncnnn
// here we need to subract 2^n - 1 - 2^a .. where a , b , c are no of consecutive characters not equal to c
long long int total = (n*(n+1))/2;
long long int concount = 0;
for(int j = 0; j < n; j++)
{
if(test[j] == c)
{
total -= (concount*(concount + 1))/2;
concount = 0;
}
else
{
concount++;
}
}
total -= (concount*(concount + 1))/2;
cout << total << endl;
}
return 0;
}
Problem Link: STRCH Problem - CodeChef