Copy and Push Back solution codechef-

Copy and Push Back solution codechef

Anton loves creating strings!

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

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

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

However, Anton doesn’t want to perform operation 11 twice in a row.

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

Input Format

  • The first line of input will contain a single integer TT, 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 NN, the length of the string AA.
    • The second line of each test case contains a string AA of length NN.

Output Format

For each test case, output on a new line the answer — YES if Anton can create AA 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.


  • 1T1051≤T≤105
  • 1N1061≤N≤106
  • AA consists of only lowercase Latin characters
  • The sum of NN across all test cases won’t exceed 106106

Sample Input 1 


Sample Output 1 



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

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

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

Test case 33: aabaab can be created as follows:

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

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

Solution – CLICK HERE

Leave a Comment