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.