📌  相关文章
📜  查找字符串的所有回文子字符串的Java程序

📅  最后修改于: 2022-05-13 01:55:44.497000             🧑  作者: Mango

查找字符串的所有回文子字符串的Java程序

给定一个字符串,任务是计算给定字符串的所有回文子串。

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"

方法:

  1. 从给定的字符串取出子串。
  2. 现在检查子串是否是回文。
  3. 如果子串是回文,则将计数加 1,否则不增加计数。
  4. 返回计数,因为它代表子字符串的数量。
  5. 打印并显示在控制台上。

例子:

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 pallindromic 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 pallindromic behavior
        for (int i = 0; i < size / 2; i++) {
  
            if (subS.charAt(i)
                != subS.charAt(size - i - 1)) {
  
                // Non-pallindromic behavior
                return false;
            }
        }
  
        // Pallindromic 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
                // pallindrome
                if (check(subString)) {
  
                    // Increment the count only if
                    // substring is pallindrome
                    count++;
                }
            }
        }
  
        // Print and display the number of substrings that
        // are pallindromic
        System.out.println(
            "No.of palindromic substrings in the given string are "
            + count);
    }
}


输出
No.of palindromic substrings in the given string are 15