# PROBLEM LINK:

* Author, Tester and Editorialist:* Jitin

# DIFFICULTY:

EASY

# PREREQUISITES:

None

# PROBLEM:

You are given a string Y and you need to check whether there is a valid string X such that

we get the same string on shifting all characters in X one place left and appending the first character to as well as shifting all characters in X one place right and adding the last character at the beginning.

# EXPLANATION:

If there is a valid X then first assume Y is the string obtained by left-shifting X. Hence original string X is obtained by right shifting Y. Similarly, another original string is obtained by left-shifting Y. If both are equal then X is valid but if they are not equal, then there doesnâ€™t exist any valid X.

# SOLUTIONS:

## C++ Solution

```
#include <iostream>
using namespace std;
string first_to_last(string x)
{
int n = x.length();
char first = x[0];
for (int i = 1; i < n; i++)
{
x[i - 1] = x[i];
}
x[n - 1] = first;
return x;
}
string last_to_first(string x)
{
int n = x.length();
char last = x[n - 1];
for (int i = n - 1; i > 0; i--)
{
x[i] = x[i - 1];
}
x[0] = last;
return x;
}
void solve()
{
int t;
cin >> t;
while (t--)
{
string y;
cin >> y;
// If there is a valid X then first assume Y is the left-shifted.
// Hence original string is obtained by right shifting it.
// Similarly another original string is obtained by left-shifting it
// If both are equal then X is valid.
string l = first_to_last(y);
string r = last_to_first(y);
if (l == r)
{
cout << "YES";
}
else
{
cout << "NO";
}
}
}
int main()
{
solve();
return 0;
}
```