Mario and the Broken String || codechef solution

 

Problem

Mario was going to gift Princess Peach a string � of even length �.

Mario was clumsy and thus, broke the string in the middle. He now has two strings � and � such that �=�[1,�2] and �=�[�2+1,�].

Mario is not sure about the order in which he should join the strings � and � to get the string �. Thus, he joins the strings in any random order. Find whether it is guaranteed that Mario will get the same string � if he joins the strings � and � in any order.

Note that �[�,�] denotes a substring of string � starting at index � and having a length (�−�+1).

Input Format

  • The first line of input will contain a single integer �, denoting the number of test cases.
  • Each test case consists of two lines of input:
    • The first line of each test case contains � - the length of the initial string �.
    • The second line contains the string �.

Output Format

For each test case, print YES if it is guaranteed that Mario will get the same string � irrespective of the order in which he joins the strings � and � and NO otherwise.

You may print each character of the string in uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).

Constraints

  • 1≤�≤3000
  • 1≤�≤1000
  • � is even.
  • � consists of lowercase english alphabets.

Sample 1:

Input
Output
4
6
abcabc
6
abcdef
4
aaaa
4
baab
YES
NO
YES
NO

Explanation:

Test case 1: On breaking, the string � gives �=��� and �=���. Thus, joining it in either way (�� or ��), would give the same string �.

Test case 2: On breaking, the string � gives �=��� and �=���. Joining it as �� would give the string ������ which is not equal to string �.

Test case 3: On breaking, the string � gives �=�� and �=��. Thus, joining it in either way (�� or ��), would give the same string �.

Test case 4: On breaking, the string � gives �=�� and �=��. Joining it as �� would give the string ���� which is not equal to string �.

Code(c++):-

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    int t;
    cin>>t;
    while(t--)
    {
     int n;
     cin>>n;
     string a;
     cin>>a;
     int i=0,j=n/2;
     int flag=0;
     while(i<n/2)
     {
     if(a[i]!=a[j])
     {
     flag=1;
     break;
    
     }
     i++,j++;
     }
     if(flag==0)
     cout<<"YES"<<endl;
     else
     cout<<"NO"<<endl;
    }
    return 0;
    
}

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-