Wednesday, March 16, 2022

Swapping Chefs Way

 Chef is working on his swap-based sorting algorithm for strings.

Given a string S of length N, he wants to know whether he can sort the string using his algorithm.

According to the algorithm, one can perform the following operation on string S any number of times:

  • Choose some index i (1iN) and swap the ith character from the front and the ith character from the back.
    More formally, choose an index i and swap Si and S(N+1i).

For example, d_cba_ can be converted to a_cbd_ using one operation where i=1.

Help Chef find if it is possible to sort the string using any (possibly zero) number of operations.

Input Format

  • 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 N, the length of the string.
  • The second line contains a string S of length N consisting of lowercase letters of the Latin alphabet.

Output Format

For each test case, print YES if it is possible to sort the string by performing any number of operations. Otherwise, print NO.

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

Constraints

  • 1T100
  • 1N103
  • Sum of N over all test cases does not exceed 2103.
  • S consists of lowercase Latin alphabets only.

Sample Input 1 

3
4
dbca
3
ccc
3
bza

Sample Output 1 

YES
YES
NO

Explanation

Test case 1: Chef can sort the string using 1 operation.

  • Choose i=1 and swap S1=d and S4=a. This way, the string becomes abcd.

Hence, the string is sorted.

Test case 2: Chef needs 0 operations to sort this string as it is already sorted.

Test case 3: It can be proven that the given string cannot be sorted using any number of operations.

No comments:

Post a Comment

मुझे दिल में बसाना आसान नहीं....

पता है Recently i realised की मुझे दिल में बसाना आसान नहीं है बात-बात पे चिढ़ जाती हूं मैं दिल लगा लूं एक बार तो फिर ज़िद पे अड़...