Sanket and strings (dsa - question practicing #strings)

Kartik Bhaiya has a string consisting of only ‘a’ and ‘b’ as the characters. Kartik Bhaiya describes perfectness of a string as the maximum length substring of equal characters. Kartik Bhaiya is given a number k which denotes the maximum number of characters he can change. Find the maximum perfectness he can generate by changing no more than k characters.

my code : (they have # in the beginning and iostream, string,cstring,cmath are the header files and #define max_size 256, I don’t know why it is not being copied)

#include
#include
#include
#include
#define max_size 256
using namespace std;

int main()
{
int maxm = 0, minm = 1000001;
int k;
cin >> k;
string s;
cin >> s;

int counter[max_size] = {0};
for (int i = 0; i < s.length(); i++)
{
    counter[s[i]]++;
    if (maxm < counter[s[i]])
    {
        maxm = counter[s[i]];
    }
}
minm = s.length() - maxm;
cout << maxm + min(minm, k);

return 0;

}

//according to me this code gives correct answer for all inputs . kindly check it once.

hello @sr7.shubhamrai

pls save ur complete code here-> https://ide.codingblocks.com/
and share its link

no this problem is not this much simple.

here u need to find the maximum length of a substring (consecutive subsequence) consisting of equal letters. and u are allowed to change no more than k characters of the original string.

for example->
4 2
abba

answer is 4 becuase can obtain both strings “aaaa”(by change 2 b’s into a) and “bbbb”(changing a’ to b)

8 1
aabaabaa
answer is 5 becuase we can obtain string “aaaaabaa” or string “aabaaaaa”. (both have 5 length substring with all smae character)

hint -> two pointers

ooohhh thank you , got now!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

CAN ANYINE TELL WHY IT’S FAILING TESTCASES…

#include
using namespace std;
int main()
{
string s;

int global_max_collective_ele=0,current_max_collective=1;

int count;
cin>>count;
cin>>s;

for(int i=0;i<s.size()-1;i++)
{
    if(s[i]==s[i+1])
    {
        current_max_collective+=1;
        global_max_collective_ele = max(current_max_collective,global_max_collective_ele);
    }
    else
    {
        current_max_collective=1;
    }
}

if(global_max_collective_ele+count<=s.size())
cout<<global_max_collective_ele+count;
else
cout<<s.size();

return 0;

}