https://www.hackerrank.com/challenges/bear-and-steady-gene/problem
Can someone please explain why my code is not passing some test cases.
include <bits/stdc++.h>
using namespace std;
string ltrim(const string &); string rtrim(const string &);
/* * Complete the 'steadyGene' function below. * * The function is expected to return an INTEGER. * The function accepts STRING gene as parameter. */
int steadyGene(string gene) {
int n =gene.length(); int req = n/4;
map<char, int >hash ;
hash['A']=0; hash['C']=0; hash['G']=0; hash['T']=0; for(auto x: gene) hash[x]++;
int ctr = 0 ;
map<char ,int >miss;
int l = 0 , r= 0;
int ans = n ;
for(auto x: hash) { ctr+=max(0 , req-x.second); miss[x.first] = max(0 , req-x.second); }
if(ctr==0) return 0 ;
map<char, int >win;
for(int i = l ; i<=r ;i++) win[gene[i]]++;
string check="ACGT"; int m =0 ;
while(r<n) { int places=0;
for(auto x: check)
{
if(miss[x]==0)
places+=win[x];
}
while(places>=ctr && l<=r)
{
if(places>=ctr)
ans = min(ans , r-l+1);
win[gene[l]]--;
l++;
places=0;
for(auto x: check)
{
if(miss[x]==0)
places+=win[x];
}
}
r++;
win[gene[r]]++;}
return ans ; }
int main() { ofstream fout(getenv("OUTPUT_PATH"));
string n_temp; getline(cin, n_temp); int n = stoi(ltrim(rtrim(n_temp))); string gene; getline(cin, gene); int result = steadyGene(gene); fout << result << "\n"; fout.close(); return 0;
}
string ltrim(const string &str) { string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;}
string rtrim(const string &str) { string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;}




