Playing with Strings Solution

 

Problem

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 S and R, each with length N, 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) such that 1 \le i, j \le N and swap the i-th and j-th character of S. 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 S to the target string R only using operations of the given type.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains a single integer N.
  • The second line contains a binary string S.
  • The third line contains a binary string R.

Output

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

Constraints

  • 1 \le T \le 400
  • 1 \le N \le 100
  • |S| = |R| = N
  • S and R will consist of only '1' and '0'

Sample 1:

Input
Output
2
5
11000
01001
3
110
001
YES
NO

Explanation:

Example case 1: Chef can perform one operation with (i, j) = (1, 5). Then, S will be "01001", which is equal to R.

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





Program :


/* jai shree ram */


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();

while (t-->0)

{

    int len=sc.nextInt();

    char arr[]=new char[len];

    char srr[]=new char[len];

    String f=sc.next();

    String s=sc.next();

    arr=f.toCharArray();

    srr=s.toCharArray();

    Arrays.sort(arr);

    Arrays.sort(srr);

    int count=0;

    for(int i=0;i<len;i++)

    {

        if(arr[i]==srr[i])

        {

            count++;

        }

    }

    if(count==len)

    {

        System.out.println("YES");

    }

    else{

        System.out.println("NO");

    }

}

}


Post a Comment

0 Comments