Wednesday, July 13, 2022

Copy and Push Back

 Anton loves creating strings!

Anton now wants to create a string S following some specific rules. They are as follows:

Initially, S is empty. Then, Anton can perform two types of operations on S:

  1. Choose a lowercase Latin character (an element of {a,b,c,,z}) and append it to S. For example, if currently S=clap, Anton can turn it into one of {clapa,clapb,,clapz}.
  2. Append a copy of S to itself. For example, if currently S=clap, Anton can turn it into clapclap.

However, Anton doesn't want to perform operation 1 twice in a row.

You are given a string A consisting of the lowercase Latin alphabet. Is it possible for Anton to create A using his operations any number of times?

Input Format

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

Output Format

For each test case, output on a new line the answer — YES if Anton can create A using his operations, and NO otherwise.

Each character of the output may be printed in either uppercase or lowercase. For example, the strings YESyes, and YeS will all be treated as identical.

Constraints

  • 1T105
  • 1N106
  • A consists of only lowercase Latin characters
  • The sum of N across all test cases won't exceed 106

Sample Input 1 

4
2
ab
3
oof
6
aabaab
5
eevee

Sample Output 1 

NO
YES
YES
NO

Explanation

Test case 1: Anton can create a by starting from the empty string and appending a using operation 1. However, there is no way to create ab — the only way to do so is to use operation 1 again and append b; but this is not allowed.

Test case 2: Anton can create oof from the empty string as follows:

  • Use operation 1 to append o. The current string is o.
  • Use operation 2 to append the string to itself. The current string is oo.
  • Use operation 1 to append f. The string is now oof, as required.

Test case 3: aabaab can be created as follows:

  • Append a to the empty string. The current string is a.
  • Use operation 2. The current string is aa.
  • Append b with operation 1. The current string is aab.
  • Use operation 2. The current string is aabaab, and we are done.

Test case 4: It can be shown that no sequence of operations will allow Anton to create the string eevee.

No comments:

Post a Comment

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

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