Given a string of lowercase ASCII characters, find all distinct continuous palindromic sub-strings of it.
Input: str = "abaaa" Output: Below are 5 palindrome sub-strings a aa aaa aba b Input: str = "geek" Output: Below are 4 palindrome sub-strings e ee g k
Step 1: Finding all palindromes using modified Manacher’s algorithm:
Considering each character as a pivot, expand on both sides to find the length of both even and odd length palindromes centered at the pivot character under consideration and store the length in the 2 arrays (odd & even).
Time complexity for this step is O(n^2)
Step 2: Inserting all the found palindromes in a HashMap:
Insert all the palindromes found from the previous step into a HashMap. Also insert all the individual characters from the string into the HashMap (to generate distinct single letter palindromic sub-strings).
Time complexity of this step is O(n^3) assuming that the hash insert search takes O(1) time. Note that there can be at most O(n^2) palindrome sub-strings of a string. In below C++ code ordered hashmap is used where the time complexity of insert and search is O(Logn). In C++, ordered hashmap is implemented using Red Black Tree.
Step 3: Printing the distinct palindromes and number of such distinct palindromes:
The last step is to print all values stored in the HashMap (only distinct elements will be hashed due to the property of HashMap). The size of the map gives the number of distinct palindromic continuous sub-strings.
Below is the implementation of the above idea.
Below are 5 palindrome sub-strings a aa aaa aba b
Count All Palindrome Sub-Strings in a String
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
- Minimum cuts required to convert a palindromic string to a different palindromic string
- 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
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Count all Prime Length Palindromic Substrings
- 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
- Split string into three palindromic substrings with earliest possible cuts
- Minimum changes to a string to make all substrings distinct
- Distinct palindromic sub-strings of the given string using Dynamic Programming
- Count of Distinct Substrings occurring consecutively in a given String
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Count of Palindromic substrings in an Index range
- Count of K-size substrings having palindromic permutations
- Count of substrings having all distinct characters
- Count of distinct substrings of a string using Suffix Trie