Find the maximum length of a symmetric substring in a string (longest palindrome)

Source: Internet
Author: User

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


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.