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
- Lexicographically all Shortest Palindromic Substrings from a given string
- Check if a string can be split into even length palindromic substrings
- Rearrange the string to maximize the number of palindromic substrings
- Permutation of given string that maximizes count of Palindromic substrings
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- 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 occurring consecutively in a given String
- Count of distinct substrings of a string using Suffix Trie
- Count distinct substrings of a string using Rabin Karp algorithm
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Count of K-size substrings having palindromic permutations
- Count of Palindromic substrings in an Index range
- Count all Prime Length Palindromic Substrings
- Make palindromic string non-palindromic by rearranging its letters
- Count of substrings having all distinct characters
- Count number of distinct substrings of a given length
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.