AMR12D - The Mirror of Galadriel

With water from the stream Galadriel filled the basin to the brim, and breathed on it, and when the water was still again she spoke. 'Here is the Mirror of Galadriel,' she said. 'I have brought you here so that you may look in it, if you will. For this is what your folk would call magic, I believe; though I do not understand clearly what they mean; and they seem also to use the same word of the deceits of the Enemy. But this, if you will, is the magic of Galadriel. Did you not say that you wished to see Elf-magic?' - Galadriel to Frodo and Sam, describing her Mirror.

We call a string S magical if every substring of S appears in Galadriel's Mirror (under lateral inversion). In other words, a magical string is a string where every substring has its reverse in the string.

Given a string S, determine if it is magical or not.


The first line contains T, the number of test cases. The next T lines contain a string each. 


For each test case, output "YES" if the string is magical, and "NO" otherwise.


1 <= T <= 100
1 <= |S| <= 10
S contains only lower-case characters.


Sample Input:

Sample Output:

Notes / Explanation of Sample Input

For the first test case, the list of substrings are : a, b, ab, ba, aba. The reverse of each of these strings is present as a substring of S too.

For the second test case, the list of substring are : a, b, ab. The reverse of "ab", which is "ba" is not present as a substring of the string.

hide comments
goelnandan: 2016-07-08 12:24:22

don't overthink.

adi_1996: 2016-07-03 20:19:12

lol :P
easy one

achint1997: 2016-06-27 13:18:34

LOL...AC in one go!!!!!!>..............

sayedathar11: 2016-06-20 14:52:00

Thinking too much over this question got me turned out to be a basic one :P

ab_bh: 2016-06-18 14:49:34

lol what a question....easy pickings ;)

vineetpratik: 2016-06-16 20:19:44

lol very easy

pamyjeela: 2016-05-30 12:47:34

This must be a joke right....

Murad Al Wajed: 2016-04-25 16:46:23

piece of cake :D

lalit_nit: 2016-04-21 12:01:11


dwij28: 2016-03-30 20:43:44

People have used binary search for this question ? massive lol :D

Added by:Varun Jalan
Time limit:2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Varun Jalan - ICPC Asia regionals, Amritapuri 2012