- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Find All Palindromic Sub-Strings of a String
In this problem, we will find all palindromic substrings of the given length.
There are two ways to solve the problem. The first way is to compare the characters of substring from start to last, and another way is to reverse the substring and compare it with the original substring to check whether the substring is palindrome.
Problem statement − We have given a string alpha. We need to find all palindromic substrings of the given string.
Sample Examples
Input
alpha = "abcd"
Output
4
Explanation
The palindromic substrings are ‘a’, ‘b’, ‘c’, and ‘d’.
Input
alpha = "pqp"
Output
4
Explanation
The palindromic substrings are ‘p’, ‘q’, ‘p’, and ‘pqp’.
Input
alpha = "abbab"
Output
8
Explanation
The palindromic substrings are ‘a’, ‘abba’, ‘b’, ‘bb’, ‘b’, ‘bab’, ‘a’, ‘b’.
Approach 1
In this approach, we will use the for loop to get all substrings of the given string. We will match the characters of the substring from the start and end to check whether the string is a palindrome. If any character doesn’t match, we can say the string is not palindromic and move ahead.
Algorithm
Step 1 − Initialize the ‘palCounts’ with 0 to store the number of palindromic strings of the given string
Step 2 − Use two nested for loops to get all substrings of the given string.
Step 3 − Use the substring() method to take a substring from the p index to the q + 1 index.
Step 4 − Execute the isPalindrome() function to check whether the substring is palindromic.
Step 4.1 − In the isPalindromic() function, use the loop to traverse the string till the ‘len/2’ index.
Step 4.2 − If the character at the ‘m’ and the ‘len - m – 1’ is not the same, return false.
Step 4.3 − Return true at last.
Step 5 − If the string is palindromic, increment the ‘palCounts’ by 1.
Step 6 − Return the ‘palCounts’.
Example
import java.io.*; public class Main { public static boolean isPalindrome(String temp) { int len = temp.length(); // Match string characters from the start and end for (int m = 0; m < len / 2; m++) { if (temp.charAt(m) != temp.charAt(len - m - 1)) { return false; } } return true; } public static void main(String[] args) { // Custom input string String alpha = "abcd"; // To store the total number of palindromic substrings int palCounts = 0; // Get all substrings starting with index p. for (int p = 0; p < alpha.length(); p++) { // Get all substrings ending at q index for (int q = p; q < alpha.length(); q++) { // Get Substring String subStr = alpha.substring(p, q + 1); // If the string is a palindrome, increment count if (isPalindrome(subStr)) { palCounts++; } } } System.out.println("The total number of plaindromic substrings of the given string is " + palCounts); } }
Output
The total number of plaindromic substrings of the given string is 4
Time complexity – O(N^3), for finding all substrings and checking whether it is palindromic.
Space complexity – O(N), to store the substring.
Approach 2
In this approach, we will use the while loop to get all substrings of the given string. After that, we will use the reverse() method to reverse the string and compare it with the original substring to check whether the substring is palindromic.
Algorithm
Step 1 − Initialize the ‘palCounts’ with 0 to store the count of the palindromic substrings.
Step 2 − Initialize the ‘p’ with 0, and traverse the string until ‘p’ is less than the length.
Step 3 − Initialize the ‘q’ with ‘p’, and traverses the string until ‘q’ is less than the string length.
Step 4 − Use the substring() method to take the substring from p to q + 1 index.
Step 5 − Use the StringBuffer() to create a string buffer and the reverse() method to reverse the string. Also, we need to use the toString() method to create the string from the string buffer.
Step 6 − use the equals() method to check whether the reverse string is the same as the original string.
Step 7 − If yes, increment the count of ‘palCounts’.
Step 8 − Return the value of ‘palCounts’.
Example
import java.io.*; public class Main { public static void main(String[] args) { // Custom input string String alpha = "abcd"; // To store the total number of palindromic substrings int palCounts = 0; int p = 0; // Get all substrings starting with index p. while (p < alpha.length()) { int q = p; // Get all substrings ending at q index while (q < alpha.length()) { // Get Substring String subStr = alpha.substring(p, q + 1); // Reverse substring String rev = new StringBuffer(subStr).reverse().toString(); // If substring and its reverse are equal if (subStr.equals(rev)) { palCounts++; } q++; } p++; } System.out.println("The total number of plaindromic substrings of the given string is " + palCounts); } }
Output
The total number of plaindromic substrings of the given string is 4
Time complexity – O(N^3) to get all substrings and reverse each substring.
Space complexity – O(N) to store the reversed substring.
The time complexity of both approaches is the same but uses different logic to check for the palindromic string. The first approach is faster than the second one as it directly compares the string character without reversing the string.