Good Prefix - PRFXGD Code Chef Easy Question
Welcome back to my blog , we will discuss another question today and it is Good Prefix.
Here is a link to the PROBLEM
So this question's solution is fairly simple , we just start iterating over the string from the start and we keep a count how many times each character has come , whenever a value exceeds X , we delete the last occurence of that character and keep a counter for the number of deletions .If we dont delete a character we increment the answer by one . When the number of deletions reach k , we stop and we got our answer.
Do look at the following code for better understanding .
Code :-
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
string s;
int arr[27]={0};
cin>>s;
//cout<<s;
int k,x;
cin>>k>>x;
int ans=0;
int i=0;
//cout<<endl;
while(s[i]!='\0')
{
//cout<<s[i]<<" ";
if(arr[(int)s[i]-97]>=x && k==0)
{
break;
}
else if(arr[(int)s[i]-97]>=x && k>0)
{
k--;
}
else
{
ans++;
arr[(int)s[i]-97]++;
}
i++;
//cout<<ans<<k<<x;
}
cout<<ans<<endl;
}
// your code goes here
return 0;
}
Do follow my blog and keep your doubts and queries in the comments section .
Comments
Post a Comment