Problem of the day
A substring is a contiguous segment of a string.
The longest palindromic substring of "ababc" is "aba", since "aba" is a palindrome and it is the longest substring of length 3 which is a palindrome. There is another palindromic substring of length 3 is "bab". Since starting index of "aba" is less than "bab", so "aba" is the answer.
The first line of input contains a single integer 'T', representing the number of test cases or queries to be run.
Then the 'T' test cases follow.
The first and only one of each test case contains a string (STR).
For every test case, print a single line containing the longest palindromic substring.
If there are multiple possible answers then you need to print the substring which has the lowest starting index.
You do not need to print anything; it has already been taken care of. Just implement the given function.
Try to solve it using O(1) space complexity.
1 <= T <= 10
0 <= N <= 10^3
where 'T' is the number of test cases, 'N' is the length of the given string.
Time Limit: 1 sec
1
abccbc
bccb
For string "abccbc" there are multiple palindromic substrings like "a", "b", "c", "cc", "bccb", "cbc". But "bccb" is of the longest length. Thus, answer is "bccb".
1
aeiou
a
For string "aeiou" there are multiple palindromic substrings like "a", "e", "I", "o", "u", and all of the same length. But palindromic substring "a" has the minimum starting index. Thus, the answer is "a".