POSON-Editorial

PROBLEM LINK:

Practice
Div-2 Contest

Editorialist: shubhamjain05

DIFFICULTY:

SIMPLE, EASY

PREREQUISITES:

String

PROBLEM:

Chef is assigned to check whether given string B can be convertible to string A by performing operation as follow :
any two characters of string can be interchange any number of times(may be 0).
If convertible print “Yes” along with number of operation required else print “No”.

QUICK EXPLANATION:

Two strings can be made identical if and only if they contains same characters in same frequency and their length must also be same and also with even number of unmatched pairs.

EXPLANATION:

Strings are said to be identical if thy have same characters in same frequency .
So ,the problem is half solved by checking the each characters’ frequency in both string A and string B .
But, here when operation(swap) is performed it considers to characters .Out of which we concluded that the number of unmatched pairs must be even and answer will be half of number of unmatched pairs .
So conditions of converting string B to A are :

  1. Same character with same frequency.
  2. Same length.
  3. Even number of unmatched pairs.

SOLUTIONS:

Setter's Solution
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string A,B;
        cin>>A>>B;
        int A_len = A.length();
        int countA = 0,countB = 0,cnt = 0;
        for(int i=0; i<A_len; i++)
        {
            if(A[i] == 'a')
                countA++;
            if(B[i] == 'a')
                countB++;
        }
        if(countA != countB)
        {
            cout<<"No"<<endl;
            continue;
        }
        for(long long i=0;i<A_len; i++)
        {
            if(A[i] != B[i]) 
            cnt++;
        }
        if(cnt%2 == 0)
            cout<<"Yes "<<cnt/2<<endl;
        else
            cout<<"No"<<endl;
    }
}