Knowee
Questions
Features
Study Tools

Write a Java program to find longest Palindromic Substring within a string CopySample Output:The given string is: thequickbrownfoxxofnworbquicktheThe longest palindrome substring in the given string is; brownfoxxofnworbThe length of the palindromic substring is: 16

Question

Write a Java program to find longest Palindromic Substring within a string CopySample Output:The given string is: thequickbrownfoxxofnworbquicktheThe longest palindrome substring in the given string is; brownfoxxofnworbThe length of the palindromic substring is: 16

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Here is a Java program that finds the longest palindromic substring within a string:

public class Main {
    static void printSubStr(String str, int low, int high) {
        System.out.println(str.substring(low, high + 1));
    }

    static int longestPalSubstr(String str) {
        int n = str.length();
        boolean table[][] = new boolean[n][n];
        int maxLength = 1;
        for (int i = 0; i < n; ++i)
            table[i][i] = true;

        int start = 0;
        for (int i = 0; i < n - 1; ++i) {
            if (str.charAt(i) == str.charAt(i + 1)) {
                table[i][i + 1] = true;
                start = i;
                maxLength = 2;
            }
        }

        for (int k = 3; k <= n; ++k) {
            for (int i = 0; i < n - k + 1; ++i) {
                int j = i + k - 1;
                if (table[i + 1][j - 1] && str.charAt(i) == str.charAt(j)) {
                    table[i][j] = true;
                    if (k > maxLength) {
                        start = i;
                        maxLength = k;
                    }
                }
            }
        }
        System.out.print("The longest palindrome substring in the given string is; ");
        printSubStr(str,

This problem has been solved

Solution 2

Here is a Java program that finds the longest palindromic substring within a string:

public class Main {
    static void printSubStr(String str, int low, int high) {
        System.out.println(str.substring(low, high + 1));
    }

    static int longestPalSubstr(String str) {
        int n = str.length();
        boolean table[][] = new boolean[n][n];
        int maxLength = 1;
        for (int i = 0; i < n; ++i)
            table[i][i] = true;

        int start = 0;
        for (int i = 0; i < n - 1; ++i) {
            if (str.charAt(i) == str.charAt(i + 1)) {
                table[i][i + 1] = true;
                start = i;
                maxLength = 2;
            }
        }

        for (int k = 3; k <= n; ++k) {
            for (int i = 0; i < n - k + 1; ++i) {
                int j = i + k - 1;
                if (table[i + 1][j - 1] && str.charAt(i) == str.charAt(j)) {
                    table[i][j] = true;
                    if (k > maxLength) {
                        start = i;
                        maxLength = k;
                    }
                }
            }
        }
        System.out.print("The longest palindrome substring in the given string is; ");
        printSubStr(str, start, start + maxLength - 1);
        return maxLength;
    }

    public static void main(String[] args) {
        String str = "thequickbrownfoxxofnworbquickthe";
        System.out.println("The given string is: " + str);
        System.out.println("The length of the palindromic substring is: " + longestPalSubstr(str));
    }
}

This program uses dynamic programming to check all possible substrings of a given string. It starts by checking all substrings of length 1 and 2, then it checks substrings of length 3 and so on. If a substring is a palindrome, it checks if it's longer than the current longest palindrome substring. If it is, it updates the start position and the length of the longest palindrome substring. Finally, it prints out the longest palindrome substring and its length.

This problem has been solved

Similar Questions

Write a function to find the longest palindromic substring in a given string

Given a string s, return the longest palindromic substring in s. Example 1:Input: s = "babad"Output: "bab"Explanation: "aba" is also a valid answer.

Find the Longest Palindromic Substring Zoe loves palindromes and she wants to find the longest palindromic substring within a given string. Can you help her find it?Constraints:NAExample:Sample Input:cbbdSample Output:bbExplanation:The longest palindromic substring from the above example is bbPublic Test Cases:# INPUT EXPECTED OUTPUT1 cbbdbb

Question3Max. score: 10.00Find the longest substring which is palindrome.Given a string, find the longest substring which is the palindrome. For example, if the given string is “forgeeksskeegfor”, the output should be “geeksskeeg”.Sample inputMadam Arora teaches MalayalamSample outputMalayalamNote:Your code must be able to print the sample output from the provided sample input. However, your code is run against multiple hidden test cases. Therefore, your code must pass these hidden test cases to solve the problem statement.LimitsTime Limit: 5.0 sec(s) for each input fileMemory Limit: 256 MBSource Limit: 1024 KB

Write a Java program to print the smallest and biggest possible palindrome word in a given string. If no palindrome is found in the given string, then, print 'no palindrome'.Input FormatIn the first line read a stringOutput FormatThe first line contains the smallest palindrome word  (or) no palindromeThe nextline contains the biggest palindrome word

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.