STONES Wrong Answer

My code looks correct. Its giving WA. Can someone help?

#include<iostream>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
              char a[110] = "", b[110] = "";
              int count1[200],count2[200];
              for(int i = 0;i < 150;i++) count1[i] = 0;
              for(int i = 0;i < 150;i++) count2[i] = 0;
              cin >> a >> b;
              for(int i = 0; a[i] != '\0';i++) count1[(int)a[i]]++;
              for(int i = 0; b[i] != '\0';i++) count2[(int)b[i]]++;
              int ans = 0;
              for(int i = 0; i < 150;i++)
                      if(count1[i] >= 1 && count2[i] >= 1) ans++;
              cout << ans << endl;
    }
    return 0;
}

The array count1[] keeps the track of the distinct characters in first string. count2[] keeps the track of 2nd. In the end we compare them. Can someone tell why it is giving WA?

2 Likes

instead of doing ans++; ,do ans=ans+count2[i]; .Here when a character is present so you will add how many times that character is present instead of incrementing the value of ans which only tells how many distinct stones are jewels.

For example J=“ab” and S=“aabca”

Your code gives answer 2 but the correct answer is 4.

Happy Coding :slight_smile:

3 Likes

@deebhatia Thanks!

1 Like
//