Javatpoint Logo
Javatpoint Logo

Find all Palindromic Sub-Strings of a given String in Java

Given 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:

Input:

Output:

The total number of distinct palindromic substrings are 8. They are:

a
b
bb
bbb
bbcbb
bcb
c
e

Example 2:

Input:

Output:

The total number of distinct palindromic substrings are 5. They are:

a
b
c
d
e

Example 3:

Input:

Output:

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:

Step 1: Declare a 2-D boolean array and must fill it in a diagonal way.

Step 2: Verify for each K.ie(0,1,2,.....)

Step 3: Assume K=0, which indicates that there is just one element. Since a single character is a palindrome, we get true in this instance.

Step 4: If K = 1, we determine whether the extremes are the same and return true; otherwise, we return false.

Step 5: If K is not the same, we check to see if the extremes are the same and see if ar[i+1][j-1] provides true; if it does, we set it to true; if not, we set it to false. We add the string to the set data structure each time a true is found.

Step 6: Return the palindromic substrings.

Implementation:

FileName: PlaindromeSubstringBrute.java

Output:

The total distinct palindromic substrings are: 8
a
b
bb
bbb
bbcbb
bcb
c
e
The total number of distinct palindromic substrings are : 8

Complexity Analysis:

The Time Complexity is O(N2 * log(N)), where 'N' represents the length of the string taken, and since it takes log(N) time to insert elements into the set as we iterate through the array. The Space Complexity is O(N2) as the 2-D array is utilized.

Approach: Using Knuth-Morris-Pratt (KMP) Algorithm

The 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:

Step 1: Declare N to initialize the length of the string "str" that is given, then initialize arr[N][N] to verify if substrings are palindromes.

Step 2: For single characters, initialize arr[i][i] to true.

Step 3: To find palindromes with a length of two, identify them adjacent to characters.

Step 4: Iterate over substrings greater than two in order to find palindromes,

Step 5: If both ends of the string match and the inner substring is a palindrome, update arr[i][j].

Step 6: To enhance palindrome detection, use the KMP method.

Step 7: To ensure accurate palindrome counting, determine prefixes and suffixes.

Step 8: Add characters to each substring as we go through them to create new substrings.

Step 9: Print the substring and increase the count if a substring is found to be a palindrome.

Step 10: Print the number of unique palindromic substrings in total.

Implementation:

FileName: plaindromeSubstringsDP.java

Output:

The total distinct palindromic substrings are:
a
b
bb
bbcbb
bcb
c
bbb
e
The total number of distinct palindromic substrings are : 8

Complexity Analysis:

The Time complexity is O(N2) and the Space complexity is O(N2) where 'N' represents the length of the given string.

Approach: Manacher's algorithm

Expand 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:

Step 1: Declare a TreeMap with the name m in order to store distinct palindromic substrings and their counts.

Step 2: Find out the input string's length, str, and initialize a 2D array row to hold palindrome lengths.

Step 3: To make palindrome recognition simpler, append special letters '@' and '#' at the start and finish of the string, respectively.

Step 4: Use two loops to iterate over the string: one for even-length palindromes and another for odd-length ones.

Step 5: Take the steps listed below for each character in the string.

Step 5.1: Check to make the palindrome around the current character larger.

Step 5.2: Update the row array's palindrome lengths.

Step 5.3: Add the palindrome substrings that have been found to the m TreeMap.

Step 6: Print every distinct palindrome substring that will remain in m.

Step 7: Determine the size of the TreeMap m in order to print the total number of unique palindromic substrings.

Implementation:

FileName: PalindromeSubstringMancherAlgo.java

Output:

The total distinct palindromic substrings are:
a
b
bb
bbb
bbcbb
bcb
c
e
The total number of distinct palindromic substrings are : 8

Complexity Analysis:

The time complexity of the above code is O(N2), where 'N' represents the length of the given string and the space complexity is O(N).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA