Background:
The so-called symmetric substring, that is, the substring is either symmetric with one of the words: "ABA", "ABCBA", or completely symmetrical: such as "ABBA", "ABCCBA".
Problem:
Give you a string that finds the maximum length of the symmetric substring in the string.
Ideas:
First, we use the character array char[] array to hold the string, assuming that we have now traversed to the I-character, to find the longest symmetric string with that character as "center", we need to move forward and backward with another two pointers, Until the pointer reaches either end of the string, or the two pointers refer to a character that is not equal. Because there are two cases of symmetric substrings, you need to write out two cases of code:
1. The I-character is the true center of the symmetric string, meaning that the symmetric string is symmetric with the I character, for example: "ABA". The code uses index to represent I.
public static int Maxlengthmiddle (char[] array, int index) {
int length = 1;//longest substring length
int j = 1;//pointer
moved forward or backward while ((array[index-j] = = Array[index + j]) && (index-j) >= 0 && array.length > (index + j)) {
length + = 2;
j + +;
}
return length;
}
2. The string i is one of the centers of a symmetric string. such as "ABBA".
public static int Maxlengthmirror (char[] array, int index) {
int length = 0;//longest substring length
int j = 0;//pointer
moved forward or backward while ((array[index-j] = = Array[index + j + 1]) && (index-j) >= 0 && array.length > (index + j + 1) {
length + = 2;
j + +;
}
return length;
}
With such two functions, we only need to traverse all the characters in the string to find the maximum length of the symmetric substring.
public static int Palindrain (char[] array) {
if (Array.Length = = 0) return 0;
int maxLength = 0;
for (int i = 0; i < Array.Length; i++) {
int tempmaxlength =-1;
int length1 = Maxlengthmiddle (array, i);
int length2 = maxlengthmirror (array, i);
Tempmaxlength = (length1 > length2)? Length1:length2;
if (Tempmaxlength > MaxLength) {
maxLength = tempmaxlength;
}
}
return maxLength;
}
The complexity of the entire algorithm is O (n^2) because it is found that the first character is the "center" symmetric string Complexity of O (N).
Here's another method algorithm, the complexity is the same, but, looks more concise.
public class Palindrome {public
static void Main (string[] args) {
palindrome PLD = new Palindrome ();
System.out.println (Pld.longestpalindrome ("ABCC"));
}
String Getpalindrome (string s, int l, int r) {
int n = s.length ();
while (l >= 0 && R <= n-1 && S.charat (l) = = S.charat (r)) {
l--;
r++;
}
Return s.substring (L + 1, R);
}
String Longestpalindrome (String s) {
int n = s.length ();
if (n = = 0) return "";
String longest = s.substring (0, 1);
for (int i = 0; i < n-1; i++) {
String p1 = Getpalindrome (s, I, I);
if (P1.length () > Longest.length ()) {
longest = P1;
}
String P2 = getpalindrome (s, I, i+1);
if (P2.length () > Longest.length ()) {
longest = p2;
}
}
return longest;
}
}
There is an algorithm that can reduce the complexity to O (N), but this algorithm uses suffix tree, which is interesting to search for.
Reference: http://www.leetcode.com/2011/11/longest-palindromic-substring-part-i.html
Reprint Please specify the Source:/http Blog.csdn.net/beiyeqingteng