Asked inOla Cabs,SDE-2
Find all distinct palindromic substrings of a given string

Ninja did not do homework. As a penalty, the teacher has given a string ‘S’ to ninja and asked him to print all distinct palindromic substrings of the given string. A string is said to be palindrome if the reverse of the string is the same as the string itself. For example, the string “bccb” is a palindrome, but the string “def” is not a palindrome.

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 sorted manner and they should be space-separated.    

Print the output of each test case in a separate line.
Constraints:
1 <= T <= 10
1 <= |S| <= 1000

String S contains lowercase English letters only.

Where ‘T’ represents the number of test cases and ‘S’ represents the given string.

Time limit: 1 sec
CodingNinjas
author
2y

Step 1 : Find all the palindromic sub-strings
Step 2 : Remove duplicate palindromes
Step 3 : Print the distinct palindromes and number of such palindromes

CodingNinjas
author
2y
Brute force

The idea is to simply use three loops and check for each substring if it is palindromic and add it to our output array accordingly. In our implementation, we will be using a Hashmap to keep...read more

CodingNinjas
author
2y
Solution using modified Manacher’s algorithm

For each character in the string, consider all odd and even lengths substrings centered at the character. For odd length substrings, keep a string and appen...read more

Add answer anonymously...
Ola Cabs SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter