Playing with Strings : PLAYSTR

Playing with Strings Codechef Solution

Public Submission link: Click here (anyone can submit here )

CU Submission link: Click here (need login by CU account)


Read problems statements in Hindi, Mandarin Chinese, Russian, Vietnamese, and Bengali as well.

Chef usually likes to play cricket, but now, he is bored of playing it too much, so he is trying new games with strings. Chef’s friend Dustin gave him binary strings SS and RR, each with length NN, and told him to make them identical. However, unlike Dustin, Chef does not have any superpower and Dustin lets Chef perform only operations of one type: choose any pair of integers (i,j)(i,j) such that 1≤i,j≤N1≤i,j≤N and swap the ii-th and jj-th character of SS. He may perform any number of operations (including zero).

For Chef, this is much harder than cricket and he is asking for your help. Tell him whether it is possible to change the string SS to the target string RR only using operations of the given type.

Input

  • The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
  • The first line of each test case contains a single integer NN.
  • The second line contains a binary string SS.
  • The third line contains a binary string RR.

Output

For each test case, print a single line containing the string "YES" if it is possible to change SS to RR or "NO" if it is impossible (without quotes).

Constraints

  • 1≤T≤4001≤T≤400
  • 1≤N≤1001≤N≤100
  • |S|=|R|=N|S|=|R|=N
  • SS and RR will consist of only ‘1’ and ‘0’

Sample Input 1

2
5
11000
01001
3
110
001

Sample Output 1

YES
NO

Explanation

Example case 1: Chef can perform one operation with (i,j)=(1,5)(i,j)=(1,5). Then, SS will be “01001”, which is equal to RR.

Example case 2: There is no sequence of operations which would make SS equal to RR.


Playing with Strings Codechef Solution in C


int main(void) {
    int t, n;
    scanf("%d", &t);

    while(t--){
        scanf("%d", &n);
        char s[n+1], r[n+1];
        int a[2] = {0}, b[2] = {0};

        scanf("%s", &s);
        scanf("%s", &r);

        for(int i=0; s[i] != '\0'; i++){
            a[s[i] - 48]++;
            b[r[i] - 48]++;
        }


        if (a[0] == b[0] && a[1] == b[1]) printf("YES\n");
        else printf("NO\n");

    }

	return 0;
}

Playing with Strings Codechef Solution in C++ 14

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int a,zero=0,one=0,zero1=0,one1=0;
        cin>>a;
        string s,s1;
        cin>>s>>s1;
        for(int i=0; i < s.size(); i++)
        {
            if(s[i]=='0')zero++;
            else if(s[i]=='1')one++;
        }
        for(int i=0; i < s1.size(); i++)
        {
            if(s1[i]=='0')zero1++;
            else if(s1[i]=='1')one1++;
        }
        if(zero==zero1 && one==one1 )cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}

Playing with Strings Codechef Solution in Python 3

# cook your dish here

T = int(input())
for i in range(0, T):
    n = int(input())
    s = str(input())
    r = str(input())
    if s.count("0") == r.count("0") and s.count("1") == r.count("1"):
        print("YES")
    else:
        print("NO")

Playing with Strings Codechef Solution in Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
		int t= sc.nextInt();
		int ones1=0,ones2=0,zeros1=0,zeros2=0;
		while(t>0)
		{
		    int n= sc.nextInt();
		    String s1 = sc.next();
		    String s2 = sc.next();
		    ones1=0;
		    ones2=0;
		    zeros1=0;
		    zeros2=0;

		    for(int j=0;j < n;j++)
		    {
		        if (s1.charAt(j)=='1')
		        ones1++;
		        else
		        zeros1++;
		    }
		    for(int j=0;j < n;j++)
		    {
		        if(s2.charAt(j)=='1')
		        ones2++;
		        else
		        zeros2++;
		    }
		    if((ones1==ones2) && (zeros1==zeros2))
		    System.out.println("YES");
		    else
		    System.out.println("NO");
		    t--;


		}
	}
}


 Playing with Strings Codechef Solution
Playing with Strings Codechef Solution
The content uploaded on this website is for reference purposes only. Please do it yourself first.