LeetCode 5 Longest Palindromic Substring (maximum echo Substring)

Source: Internet
Author: User

LeetCode 5 Longest Palindromic Substring (maximum echo Substring)

Translation

Given a string S, find its maximum echo substring. You can assume that the maximum length of S is 1000, and there is a unique maximum echo substring.

Original

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.

Brute force search, O (n3)

public static string LongestPalindrome(string s){    int len = s.Length;    for (int i = len; i > 0; i--)        for (int j = 1; j <= len + 1 - i; j++)            if (isPalindrome(s.Substring(j - 1, i)))                return s.Substring(j - 1, i);    return null;}public static bool isPalindrome(string s){    for (int i = 0; i < s.Length / 2; i++)        if (s.Substring(i, 1) != s.Substring(s.Length - 1 - i, 1))            return false;    return true;}

Dynamic Planning, time: O (n2) , Space: O (n2)

Public class Solution {public string LongestPalindrome (string s) {int sLen = s. length; int lonBeg = 0; int maxLen = 1; bool [,] DP = new bool [1000,100 0]; for (int I = 0; I <sLen; I ++) {DP [I, I] = true;} for (int I = 0; I <sLen-1; I ++) {if (s [I] = s [I + 1]) {DP [I, I + 1] = true; lonBeg = I; maxLen = 2 ;}} for (int len = 3; len <= sLen; len ++) // All substrings whose lengths start from 3 {for (int I = 0; I <sLen + 1-len; I ++) {int j = len-1 + I; // j is the index at the end of the array. if (s [I] = s [j] & DP [I + 1, j-1]) {DP [I, j] = true; // from I to j, return lonBeg = I; // lonBeg is the starting index, which is equal to I maxLen = len; // maxLen is the string length }}return s. substring (lonBeg, maxLen );}}

However, the tragedy continues ......

Submission Result: Memory Limit Exceeded
public class Solution{    public string LongestPalindrome(string s)    {        int nLen = s.Length;        if (nLen == 0) return ;        string lonStr = s.Substring(0, 1);        for (int i = 0; i < nLen - 1; i++)        {            string p1 = ExpandAroundCenter(s, i, i);            if (p1.Length > lonStr.Length)                lonStr = p1;            string p2 = ExpandAroundCenter(s, i, i + 1);            if (p2.Length > lonStr.Length)                lonStr = p2;        }        return lonStr;    }    public static string ExpandAroundCenter(string s, int n, int m)    {        int l = n, r = m;        int nLen = s.Length;        while (l >= 0 && r <= nLen - 1 && s[l] == s[r])        {            l--;            r++;        }        return s.Substring(l + 1, r - l - 1);    }}

Okay, this method comes from the network ...... It is said that O (n2) Time, but only O (1) Space!

 

Related Article

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.