Help me in solving STONES problem

My issue

I compared both the strings, and stored the common values in another vector. Now i sort the new vector, and check only the unique values, by increasing the count. Then also, if none common value was found i reduce count to zero. So, why am i getting WA??

My code

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>
using namespace std;
// Remember *max_element(c,c+n)

#define ll long long
#define shit int t;cin>>t;while(t--)
#define pi 3.141597653
#define o 0
#define l 1
#define el endl
#define loop(j,a,b) for(int j=a; j<b; j++)
#define inn int n;cin>>n
#define sinn string s;cin>>s
#define loopaa() int a[n]; for(int i=0;i<n;i++){ cin>>a[i]; }

int main(){
    shit{
        string j; cin>>j;
        string s; cin>>s;
        int c=1;
        vector<int> b;
        loop(i,o,j.length()){
            loop(k,o,s.length()){
                if(s[k]==j[i]){
                    b.push_back(s[k]);
                }
            }
        }
        sort(b.begin(), b.end());
        loop(i,1,b.size()){
            if(b[i]!=b[i-1]){
                c++;
            }
        }
        if(b.size()==0){
            c=0;
        }
        cout<<c<<el;
        
        
        
        
        
    }
    
    
    
    
    
    
}


Problem Link: STONES Problem - CodeChef

@umang03r
for test case
abc
aabc
the answer would be 4 but your code will print 3.

1 Like

Just count the frequency of each stone in s using a map. Then, for each jewel in J, it checks if there is any available stone and adds the count to ans. After using the stone, its count is set to 0. This way, you efficiently count the number of jewels that can be formed from the stones.