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:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find distinct characters in distinct substrings of a string
- Lexicographically all Shortest Palindromic Substrings from a given string
- Rearrange the string to maximize the number of palindromic substrings
- Permutation of given string that maximizes count of Palindromic substrings
- Minimum changes to a string to make all substrings distinct
- Find all distinct palindromic sub-strings of a given string
- Count of distinct substrings of a string using Suffix Array
- Count of distinct substrings of a string using Suffix Trie
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Count all Prime Length Palindromic Substrings
- Count of Palindromic substrings in an Index range
- Make palindromic string non-palindromic by rearranging its letters
- Count distinct substrings that contain some characters at most k times
- Count number of distinct substrings of a given length
- Count number of substrings with exactly k distinct characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.