LeetCode [5]. Longest Palindromic Substring, longestsubstring

Source: Internet
Author: User

LeetCode [5]. Longest Palindromic Substring, longestsubstring

Longest Palindromic Substring

I. Questions: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
The topic requires the maximum symmetric substring of a given string. For example, the maximum symmetric substring of "aaabccbacc" is "abccba ". Ii. Train of Thought: Compare each character or two characters in the center to the two sides and determine whether it is a symmetric subcharacter. The idea is simple. The two situations are as follows:
Figure 1 and two symmetric conditions. For symmetric strings, the above two conditions may occur. One is A "even symmetric", and the middle line is symmetric. The other is B "odd symmetric", with A character in the middle as the center symmetric. Always scan from 0 to the end of the string through I, move forward 0.5 each time, and take I as the center, scanning to both ends for judgment. The time complexity is O (n ^ 2), and the worst case is that all characters in the entire string are the same. Therefore, each scan has to reach the end. Iii. Java program
</Pre> <pre name = "code" class = "java"> public class Solution {public String longestPalindrome (String s) {int down = 0, up = 0, count = 0; // record the upper and lower intervals of the string and the length of the string int sl = s. length (); int maxl = 0; int subDown = 0, subUp = 0; // upper and lower intervals of the target substring String subS = new String (""); if (sl = 0) return ""; // 1. locate the middle position and upper and lower limits of the target substring for (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) // determine the status of I (A or B) {count = 0;} else {count = 1; down --; up ++;} // 2. scan the while (! (Down <0 | up> sl-1) {if (s. charAt (down )! = (S. charAt (up) {break; // 2.1. if the I-centric symmetric two-character characters are not equal, the scan jumps out} else {// 2.2. if the I-centered symmetric two-character characters are equal, continue to move count + = 2; down --; up ++ ;}}/3. update the longest string if (count> maxl) {maxl = count; subDown = down; subUp = up ;}// 3.1 because the string is moved before being judged during scanning, in fact, one unit is moved before and after the stop. Here, the unit is restored to subDown ++; subUp --; subS = s. substring (subDown, subUp + 1); return subS; // return symmetric substring }}


Extended learning: This article introduces several methods of Java Longest Palindromic Substring (Longest return string)

Leetcode-Longest Palindromic Substring (Java)




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.