Find all Palindromic Sub-Strings of a given String in Java10 Sept 2024 | 8 min read 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 AlgorithmAlgorithm: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) 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: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 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: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). |
To achieve parallelism, Java developers sometimes have to decide between multiprocessing and multithreading. Every one of these approaches has benefits and drawbacks, so understanding how they vary from one another can help we select the optimal approach for your specific need. Multithreading in Java The process of dividing...
3 min read
In this section, we will discuss how to rotate a matrix by 180 degree in Java. In this problem, a square matrix is given, and we have to rotate it by 1800. Example 1: Input: 4 6 7 8 9 3 2 1 9 0 4 5 8 0 3 2 Output: 2 3...
10 min read
The java.nio.FloatBuffer Class's has slice() function .Using the FloatBuffer Class, a new float buffer with a shared subsequence of the content of the given buffer can be created. The present position of this buffer will be used as the starting point for the content of the...
2 min read
An error defines a reasonable issue that is topping the execution of the program. In different programming languages, there occur different types of errors as per the concepts. This section will discuss errors in Java and different types of errors, and when such errors occur. What is an...
4 min read
Constructors in Java A constructor in Java is similar to a method with a few differences. Constructor has the same name as the class name. A constructor doesn't have a return type. A Java program will automatically create a constructor if it is not already defined in the...
4 min read
In this section, we will learn what is a partition number and also create Java programs to check if the given number is a partition number or not. The partition number program is frequently asked in Java coding interviews and academics. Partition Number In combinatorics and number theory,...
4 min read
The Rotate Bits problem involves shifting the bits of an integer to the left or right, wrapping the overflowed bits to the opposite end. This operation is crucial in low-level programming, cryptography, and data manipulation tasks. Java provides bitwise operators to implement this efficiently for both...
7 min read
? In Java programming, an enum (quick for enumeration) is a special facts kind that permits you to define a fixed of named constants. Enum constants are essentially predefined values that can be used to symbolize a particular set of values, like the days of the week,...
10 min read
Branching statements are used to change the flow of execution from one section of a program to another. Branching statements are typically utilized within control statements. Java includes three types of branching statements: continue, break, and return. When a given condition is met, we can depart...
7 min read
The word Core describes the basic concept of something, and here, the phrase 'Core Java' defines the basic Java that covers the basic concept of Java programming language. We all are aware that Java is one of the well-known and widely used programming languages, and to...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India