Last Updated: 30 Jul, 2021

Palindromic Substrings

Moderate
Asked in companies
OracleTower Research CapitalAmdocs

Problem statement

You are given a string ‘S’. Your task is to return all distinct palindromic substrings of the given string in alphabetical order.

A string is said to be palindrome if the reverse of the string is the same as the string itself.

For Example:
Consider ‘S’ = ‘abba’, all the possible substrings are [ ‘a’, ‘ab’, ‘abb’, 'abba', 'b', ‘ba’, 'bb', ‘bba’ ] out of which [ ‘a’, ‘abba’, 'b’, 'bb'] are palindromic substrings.
Input Format:
The first line of input contains an integer ‘T’, denoting the number of test cases.

The first and only line of each test case contains the string ‘S’.
Output Format:
For each test case, print all distinct palindromic substrings of the given string. Print the substrings in a sorted manner and they should be space-separated.    

Print the output of each test case in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= |S| <= 1000

String 'S' contains lowercase English letters only.

Time limit: 1 sec