WA in PRFXGD

@vijju123
my approach:
create a multi set. check everytime a char is entered if its count is less than x increase counter else do k-- as long as k isn’t -1 else break . unable to see why I’m getting wa

#include <bits/stdc++.h>

using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	
		string s;
		cin>>s;
		int k,x;
		cin>>k>>x;
		long long counter=0;
		multiset <string> stringSet;
		unordered_map <string,int> um1;
		
		for(int i=0;i<s.length();i++)
		{
			if(stringSet.find(string(1,s[i])) == stringSet.end() && stringSet.count(string(1,s[i]))<=x)
			{
				stringSet.insert(string(1,s[i])) ;
				counter++;
			}
			else
			{
				k--;
				if(k==-1)	
					break;
			
			}
		}
		
		cout<<counter<<"\n";
		
		
		
	}
}
	

Doesnt your code always print N? You seem to be entering and checking frequency of prefix rather than characters.

sample test cases passed so its definitely not always n.the for loop runs till the length of string
so before i insert a char i see if it exists and if it does is its count less than x and add counter. if not i subtract k.
if i have no k to subtract i break the program ending the for loop

my string(1,s[i]) is just to make it compatible so that char and str dont mismatch

I noticed your condition to increment answer is-

  • Searching for that char should yield end of container. Meaning the char should have frequency 0.
    AND
  • The freq should be <=x

Your first condition is wrong. Please see if it helps

1 Like

that was the problem. thank you very much