Palindrome
InterviewTime limit1sMemory limit256 MB
From the given words, pick two at different positions so their concatenation is a palindrome and report the lexicographically smallest one.
- Level
Easy3 of 10
- Topics
- Brute force, String, Two pointers
- Solved
- No attempts yet
Problem
A palindrome is a word that reads the same forwards and backwards. For example, civic, radar, rotor, and madam are palindromes.
A notebook contains words made of lowercase letters. You want to pick two words at different positions and concatenate them in order so that the result is a palindrome. That is, for two distinct indices , check whether the string formed by appending the -th word right after the -th word is a palindrome. (The same word cannot be used twice.)
For example, when the words are aaba, ba, ababa, bbaa, and baaba, appending ba after ababa forms the palindrome abababa.
Given words, write a program that finds such a palindrome.
Input
The first line contains the number of test cases . For each test case, the first line contains the number of words () written in the notebook. Each of the next lines contains one word made of lowercase letters from a to z. Within a single test case, the sum of the lengths of all words is at most 10,000.
Output
For each test case, print a palindrome that can be formed, one per line. If several palindromes are possible, print the lexicographically smallest one. If no palindrome can be formed, print 0.