Longest palindromic Substring
first, the topic is as follows:Given A stringS, find the longest palindromic substring inS. Assume that maximum length ofSis, and there exists one unique longest palindromic substring.
The topic requires the maximum symmetric substring of a given string, such as "AAABCCBACC", the maximum symmetric substring of "ABCCBA". second, the idea:compares the past between each character or two characters in the middle as a center, and determines whether it is a symmetric sub-character. The idea is simple, two situations are as follows:
figure one, two symmetry cases, there are two possible cases for symmetric strings. One is a "even symmetry", with the middle spacer line symmetrical to each other, and the other is B "odd symmetry", with the center symmetry of the middle one character. Through I from 0 to the tail of the string, each advance 0.5, and I as the center, sweep to the ends of the judgment. The time complexity is O (n^2), the worst case for the whole string is the same as all characters, then each sweep will be swept to the end. Third, Java program
</pre><pre name= "code" class= "Java" >public class solution {public String longestpalindrome (String s) { int down = 0,up = 0,count = 0; The upper and lower bounds of the sweep string and the length of the string int sl = S.length (), respectively; int MAXL = 0; int subdown = 0, subup = 0; The upper and lower bounds of the target substring string subS = new String (""); if (sl==0) return ""; 1. Find the middle position and upper and lower bounds for the target substring (double i=0; i<=sl-1; i=i+0.5) {down = (int) math.floor (i); up = (int) math.ceil (i); if ((i%1==0.5) &&sl!=0)//Judge I belongs to (a or b) {count = 0; }else {count = 1; down--; up++; }//2. Scan both sides with the I as the Center (!) down<0| | UP>SL-1) {if (S.charat (down)! = (S.charat (UP))} {break; 2.1. When the two characters with the center symmetry of I are unequal, they jump out of the scan}else{ 2.2. When I is the center symmetry of the two characters typeface, and so on, continue to move count+=2 respectively to both sides; down--; up++; }}//3. Update the longest string if (COUNT>MAXL) {Maxl = count; Subdown = down; Subup = up; }}//3.1 because in the scan, the first move and then judge, stopped in fact, before and after moving a unit, where a unit of recovery subdown++; subup--; SubS = s.substring (subdown,subup+1); Return subs;//returns the symmetric substring}}
Extended Learning: This article describes several methods of Java longest palindromic Substring (the longest palindrome string)
leetcode–longest palindromic Substring (Java)
Leetcode "5". Longest palindromic Substring--java implementation