Java longest palindromic Substring (longest palindrome string)

Source: Internet
Author: User

Suppose a string is written from left to right and right-to-left, and this string is called palindromic string. such as ABA, or ABBA. The subject is this, given the input of a string. Requires that a substring be output so that the substring is the longest padromic string.

3 ideas are available below

1. Two side comparison method

Take ABBA such a string for example, in Abba, a common own even number of words. 1th digit = Last 1th place. 2nd digit = Last 2nd place ... Nth bit = Countdown nth
The ABA is an example of a string such as ABA. A common possession of an odd number of characters. After excluding the middle character, the 1th bit = the 1th place ... Nth bit = Countdown nth
So, if we find a substring of length len1, we test it to see if it is satisfied, and the 1th bit = the 1th digit. 2nd digit = Last 2nd place ... The nth bit = the nth digit of the countdown. In other words, to test the head to the midpoint, the characters are equal to each other.

public class LongestPalindromicSubString1 {/** * @param args */public static void main (string[] args) {//TODO Auto-genera Ted Method StubSystem.out.println (longestPalindrome1 ("BABCBABCBACCBA"));} public static string LongestPalindrome1 (string s) {int maxpalinlength = 0; String Longestpalindrome = null;int length = s.length ();//Check all possible sub Stringsfor (int i = 0; i < length; i+ +) {for (int j = i + 1; j < length; J + +) {int len = j-i; String Curr = S.substring (i, J + 1), if (Ispalindrome (Curr)) {if (len > maxpalinlength) {longestpalindrome = Curr;maxpal Inlength = Len;}}} return longestpalindrome;} public static Boolean ispalindrome (String s) {for (int i = 0; i < s.length ()-1; i++) {if (S.charat (i)! = S.charat (S.L Ength ()-1-i)) {return false;}} return true;}} </span>

2. Dynamic Programming Method

If the value of dp[I [j] is true, the substring representing the characters from I to J in the string S is a palindrome string. Then be able to launch:
dp[I [j] = dp[i + 1][j-1] && s[i] = = s[J].
This is the general case, because it is necessary to rely on i+1, j-1, so it is possible i + 1 = j-1, I +1 = (j-1)-1, so the benchmark should be required to apply the above formula:
A. i + 1 = j-1, i.e. palindrome length is 1 o'clock, dp[I [i] = true;
b. I +1 = (j-1)-1, i.e. palindrome length is 2 o'clock, dp[I [i + 1] = (s[i] = = s[i + 1]).
With the above analysis, you can write the code.

It is important to note that dynamic planning requires additional O (N2) space.

public class LongestPalindromicSubString2 {public static String longestPalindrome2 (string s) {if (s = = null) return null; F (s.length () <=1) return s; int maxlen = 0; String longeststr = null; int length = S.length (); int[][] table = new Int[length][length]; Every is palindromefor (int i = 0; i < length; i++) {table[i][i] = 1;} PrintTable (table); e.g. bcba//two consecutive same letters is palindromefor (int i = 0; I <= length-2; i++) {//system.out.println ("i=  "+i+" "+s.charat (i)"//system.out.println ("i=" +i+ "" +s.charat (i+1)); if (S.charat (i) = = S.charat (i + 1)) {table[i][i + 1] = 1;longeststr = S.substring (i, i + 2);}}  System.out.println (LONGESTSTR);p rinttable (table)//condition for calculate whole tablefor (int l = 3; l <= length; l++) {for (int i = 0; I <= length-l; i++) {Int J = i + l-1;if (S.charat (i) = = S.charat (j)) {Table[i][j] = table[i + 1][j -1];if (Table[i][j] = = 1 && l > MaxLen) longeststr = S.substring (i, j + 1);} else {Table[i][j] = 0;} PrintTable (table);}} return LONGESTSTR;} public static void PrintTable (int[][] x) {for (int. [] y:x) {for (int z:y) {//system.out.print (z + "");} System.out.println ();} SYSTEM.OUT.PRINTLN ("------");} public static void Main (string[] args) {System.out.println (longestPalindrome2 ("1263625"));//babcbabcbaccba}}</ Span>

3. Central Expansion method

Since palindrome strings are symmetric on the central axis, let's say we start with subscript i. With 2 pointers extending to both sides of I to infer equality, then only 0 to
By doing this, the n-1 subscript will be able to find the longest palindrome string. However, it is important to note that there are 2 types of "ABCBA" and "ABBA" in the case of a string with odd-even symmetry.
Therefore, you need to make inferences when writing your code.
Set function int palindromic (string &s, int i, int j) is the length of the palindrome that is extended by subscript I and J, then subscript 0 to N-1. Call this function 2 times:
int lenodd = palindromic (str, I, i) and int leneven = palindromic (str, I, j), we can get the substring length labeled as odd palindrome and even palindrome by I.


Next, the maximum values in lenodd and Leneven are comparable to the current Max Max.
One advantage of this approach is that the time complexity is O (N2) and no additional space is required.

public class LongestPalindromicSubString3 {public static String Longestpalindrome ( String s) {if (S.isempty ()) {return null;} if (s.length () = = 1) {return s;} String longest = s.substring (0, 1); for (int i = 0; i < s.length (); i++) {/get longest palindrome with center of Istri ng tmp = Helper (s, I, I), if (Tmp.length () > Longest.length ()) {longest = tmp;} Get longest palindrome with center of I, I+1tmp = helper (s, I, i + 1), if (Tmp.length () > Longest.length ()) {Longest = tmp;}} return longest;} Given a center, either one letter or letter,//Find longest palindromepublic static string helper (string s, int beg In, int end) {while (Begin >= 0 && end <= s.length ()-1&& s.charat (begin) = = S.charat (end)) {begin- -;end++;} String SubS = s.substring (begin + 1, end); return SubS;} public static void Main (string[] args) {System.out.println (Longestpalindrome ("ABCCBA"));//babcbabcbaccba}}</span 

Java longest palindromic Substring (longest palindrome string)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.