Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 are given a String and our task is to find all palindromic sub-strings of the specified length. There are two ways to solve the problem. The first way is to compare the characters of sub-string from start to last, and another way is to reverse the sub-string and compare it with the original sub-string to check whether it is palindrome.
String in Java is a class which represents a sequence of characters. It is immutable which means once it is created a String object cannot be changed. And, sub-string is a small portion or part of a string.
Example Scenario 1:
Input: string str = "abcd" Output: res = 4
The palindromic substrings are 'a', 'b', 'c', and 'd'.
Example Scenario 2:
Input: string str = "abbab" Output: res = 8
The palindromic substrings are 'a', 'abba', 'b', 'bb', 'b', 'bab', 'a', 'b'.
Using for Loop
In this approach, we will use the for loop to get all sub-strings of the given string. We will match the characters of the sub-string 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.
Example
The following Java program shows how to find palindromic sub-strings of a given string using for loop.
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 palindromic substrings of the given string is " + palCounts);
}
}
On running this code, it will generate the following result
The total number of palindromic substrings of the given string is 4
Following are the time and space complexities
//To find all substrings and checking whether it is palindromic. Time complexity ? O(N^3) //To store the substring Space complexity ? O(N)
Using while Loop
In this approach, we will use the while loop to get all sub-strings of the given string. After that, we will use the reverse() method to reverse the string and compare it with the original sub-string to check if it is palindromic or not.
Example
In this Java program, we use the while loop to print find palindromic sub-strings of a given string.
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 palindromic substrings of the given string is " + palCounts);
}
}
Output of the above code is as follows ?
The total number of palindromic substrings of the given string is 4
Following are the time and space complexities
//To get all substrings and reverse each substring Time complexity ? O(N^3) //To store the reversed substring Space complexity ? O(N)