Thursday, September 4, 2025
HomeLanguagesJavaJava Program to Find All Palindromic Sub-Strings of a String

Java Program to Find All Palindromic Sub-Strings of a String

Given a string, the task is to count all palindrome substring in a given string.

Input        : aba
Output       : 4
Explanation  : All palindrome substring are : "aba" , "a" , "b", "a"

Input        : TENET
Output       : 7
Explanation  : All palindrome sub-string are : "T" , "E" , "N", "E", "T" , "ENE" , "TENET"

Approach:

  1. Take the substrings from the given string.
  2. Now check whether the substring is Palindrome or not.
  3. If the substring is Palindrome increment the count by 1 else if not count is not incremented.
  4. Return the count as it represents the number of substrings.
  5. Print and display on the console.

Example:

Java




// Java Program to Find all palindromic sub-strings
// of a given string
 
// Importing input output classes
import java.io.*;
 
// Main class
// To check for palindromic sub-strings
public class GFG {
 
    // Method 1
    // To check for substring
    public static boolean check(String subS)
    {
        int size = subS.length();
 
        // Iterating over string till midway to
        // check palindromic behavior
        for (int i = 0; i < size / 2; i++) {
 
            if (subS.charAt(i)
                != subS.charAt(size - i - 1)) {
 
                // Non-palindromic behavior
                return false;
            }
        }
 
        // Palindromic behavior
        return true;
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Custom input string
        String str = "MALAYALAM";
 
        int count = 0;
 
        // Outer loop iterating over input string
        for (int i = 0; i < str.length(); i++) {
 
            // Inner loop iterating from current starting
            // character of outer loop current starting
            // character
            for (int j = i; j < str.length(); j++) {
 
                // Getting the substrings
                String subString = str.substring(i, j + 1);
 
                // Checking whether the substring is
                // palindrome
                if (check(subString)) {
 
                    // Increment the count only if
                    // substring is palindrome
                    count++;
                }
            }
        }
 
        // Print and display the number of substrings that
        // are palindromic
        System.out.println(
            "No.of palindromic substrings in the given string are "
            + count);
    }
}


Output

No.of palindromic substrings in the given string are 15
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS