JAVA code - algorithm basis: given a string to find the longest palindrome substring

Keywords: Java

Find the longest palindrome substring given a string

Enter a string to find the longest palindrome substring.
The meaning of substring is: a string segment that appears continuously in the original string.
Palindrome means that the substring is the same when viewed from left to right and from right to left, for example: abba, yyxyy.
All punctuation and spaces are ignored in judgment, and case is ignored, but the output should remain the same.
The length of the input string does not exceed 5000 and occupies a single line.
The longest palindrome string should be output. If there are more than one, output the left most starting position.

Input example:

fafadabcbafdfdfas
fafdafayyxyyffdadaa
afdfdafabcabcbafdfdafxxxxxxxxyyyyxyyxxyyx

Output results:

afdfdfa
yyxyy
xxxxxxxx

package com.bean.algorithmexec;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class LongestPalindromeString {

    /*
     * Enter a string to find the longest palindrome substring. 
     * The meaning of substring is: a string segment that appears continuously in the original string.
     * Palindrome means that the substring is the same when viewed from left to right and from right to left, for example: abba, yyxyy. 
     * All punctuation and spaces are ignored in judgment, and case is ignored, but the output should remain the same.
     * The length of the input string does not exceed 5000 and occupies a single line. 
     * The longest palindrome string should be output. If there are more than one, output the left most starting position.
     * 
     * 
     */

    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub

        System.setIn(new FileInputStream("G:\\LongestPalindromeString.txt"));

        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {

            int max=0;//Record maximum
            int x=0;//Record the subscript of the original location
            int y=0;//Record the subscript of the original location
            //Read the input string
            String strdemo=sc.nextLine();
            //According to the length of the read string, the integer array p is constructed to record the original position of the character
            int[] p=new int[strdemo.length()];
            //According to the read string length, construct charArray, which is used to record the characters after removing punctuation, space, etc
            char[] charArray=new char[strdemo.length()];
            //Remove punctuation, spaces, etc          
            for (int i = 0,m = 0; i < strdemo.length(); i++) {
                if (Character.isLetter(strdemo.charAt(i))) {
                    p[m] = i;
                    charArray[m++] = Character.toLowerCase(strdemo.charAt(i));
                }

            }

          //Start to find the palindrome string, and use the reverse of StringBuilder to compare
          //The basic idea is violence
            String strTemp = new String(charArray).trim();
            for (int i = 0; i < strTemp.length(); i++) {
                for (int j = i+1; j < strTemp.length(); j++) {
                    StringBuilder builder = new StringBuilder(strTemp.substring(i, j));
                    //When the positive and negative are the same, it means palindrome string
                    if (builder.toString().equals(builder.reverse().toString())) {
                        if (j-i > max) {
                            max = j - i;
                            x = i;
                            y = j;
                        }
                    }
                }
            }
            System.out.println(strdemo.substring(p[x],p[y]));

        }
    }

}

(end)

Posted by SamiBH on Sun, 03 May 2020 03:52:09 -0700