## Find all Palindromic Sub-Strings of a given String in JavaGiven a string str, our task is to find the substrings that are to be in the form of palindromes such that they should be all distinct palindromic substrings of the given string. ## Example 1:
The total number of distinct palindromic substrings are 8. They are: a b bb bbb bbcbb bcb c e ## Example 2:
The total number of distinct palindromic substrings are 5. They are: a b c d e ## Example 3:
The total number of distinct palindromic substrings are 11. They are: i ippi issi ississi m p pp s sis ss ssiss ## Approach: Using Brute-Force Algorithm## Algorithm:
## Implementation:
The total distinct palindromic substrings are: 8 a b bb bbb bbcbb bcb c e The total number of distinct palindromic substrings are : 8
The Time Complexity is O(N ## Approach: Using Knuth-Morris-Pratt (KMP) AlgorithmThe KMP technique is adapted in this method to effectively find palindromic substrings. It finds prefixes and suffixes that match by iteratively going through the string and comparing the letters. The KMP array is updated to track the length of the matching prefix upon the identification of a matching suffix and prefix. The matching element in the palindrome array is set to false if a palindrome is found, preventing it from being calculated more than once. By removing the unnecessary counting of palindromic substrings, this optimization yields a precise count of unique palindromes. ## Algorithm:
## Implementation:
The total distinct palindromic substrings are: a b bb bbcbb bcb c bbb e The total number of distinct palindromic substrings are : 8
The Time complexity is O(N ## Approach: Manacher's algorithmExpand on both sides, considering each letter as a pivot, to determine the length of even and odd length palindromes centred at the pivot character in the query. Store the result in the two arrays (odd & even). Place each and every palindrome that was identified in the preceding phase into a hash map. In order to create unique single-letter palindromic sub-strings, insert each individual character from the string into the HashMap as well. The final step is to print every value that has been stored in the hash map (because of a hash map attribute, only distinct items will be hashed). The number of unique palindromic continuous sub-strings can be determined by looking at the size of the map. ## Algorithm:
## Implementation:
The total distinct palindromic substrings are: a b bb bbb bbcbb bcb c e The total number of distinct palindromic substrings are : 8
The time complexity of the above code is O(N |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]