Given a string str of lowercase alphabets, the task is to find all distinct palindromic sub-strings of the given string.
Input: str = “abaaa”
Palindromic sub-strings are “a”, “aa”, “aaa”, “aba” and “b”
Input: str = “abcd”
Approach: The solution to this problem has been discussed here using Manacher’s algorithm. However we can also solve it using dynamic programming.
Create an array dp where dp[i][j] is set to 1 if str[i…j] is a palindrome else 0. After the array has been generated, store all the palindromic sub-strings in a map in order to get the count of distinct sub-strings.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Generate a String of having N*N distinct non-palindromic Substrings
- Find distinct characters in distinct substrings of a string
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Permutation of given string that maximizes count of Palindromic substrings
- Lexicographically all Shortest Palindromic Substrings from a given string
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Make palindromic string non-palindromic by rearranging its letters
- Rearrange the string to maximize the number of palindromic substrings
- Check if a string can be split into even length palindromic substrings
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Split string into three palindromic substrings with earliest possible cuts
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Find all distinct palindromic sub-strings of a given string
- Count of distinct substrings of a string using Suffix Trie
- Count of distinct substrings of a string using Suffix Array
- Count distinct substrings of a string using Rabin Karp algorithm
- Count of Distinct Substrings occurring consecutively in a given String
- Count of Palindromic substrings in an Index range
- Count all Prime Length Palindromic Substrings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.