Leetcode the longest substring palindromic

Title Description

Given a string s, s to find the longest substring palindromic. You can assume that the maximum length of 1000 s.

Thinking

  1. Violence Solution: The flip obtained rev s, s from the beginning in the rev elements match, a match is verified from the start position to match or do not match up to reach the end of the string, the string is matched segment, this time is necessary to determine the string whether a string is a palindrome, if the record, if not then jump out. Obviously, this method has the complexity O (n ^ 3), part of the case will timeout
  2. Dynamic programming, the establishment of a matrix to describe whether j is a palindrome string from the string of i to a location, if compared with 1, 0 if otherwise, should be noted that whether i to j palindrome string, first need to i and j of the same character, followed by the required distances j and i is less than 3 or i + 1 to j-1 is a palindromic sequence. If palindromic substrings to start and length, the final output is taken recorded. Specific reference
  3. Center diffusion method with particular reference to

Code

Solution one:

class Solution {
public:
    string longestPalindrome(string s) {
        if(s.length()==1) return s;
        string rev = s;
        string res;
        reverse(rev.begin(),rev.end());
        int max_len = 0;
        for(int i = 0; i <s.length();i++)
        {
            for(int j = 0;j < rev.length();j++)
            {
                if(s[i] == rev[j])
                {
                    string tmp;
                    int k = i;
                    int m = j;
                    while(s[k] == rev[m] && k < s.length() && m < s.length())
                    {
                        tmp = tmp + s[k];
                        k++;
                        m++;
                    }
                    string tmp_rev = tmp;
                    reverse(tmp_rev.begin(),tmp_rev.end());
                    if(tmp == tmp_rev)
                    {
                        if(tmp.length()>max_len)
                        {
                        max_len = tmp.length();
                        res = tmp;
                        }
                    }
                    
                }
            }
        }
        return res; 
    }
};

Solution two:

class Solution {
public:
    string longestPalindrome(string s) {
        if(s.length()<2) return s;
        int len = s.length();
        vector<vector<int>> D(len,vector<int>(len));
        for(int i = 0; i < len;i++)
            D[i][i] = 1;
        int max_len = 1;
        int start = 0;
        for(int j = 1; j < len;j++)
        {
            for(int i = 0;i<j;i++)
            {
                if(s[i] == s[j])
                {
                    if(j-i<3)
                        D[i][j] = 1;
                    else
                    {
                        D[i][j] = D[i+1][j-1];
                    }
                }
                else
                    D[i][j] = 0;
                if(D[i][j])
                {
                    if(j-i+1>max_len)
                    {
                        max_len = j-i+1;
                        start = i;
                    }
                }
            }
        }
        return s.substr(start,max_len);
    }
};

Method three:

class Solution {
public:
    string longestPalindrome(string s) {
        if(s.length()<2) return s;
        int len = s.length();
        int maxLen = 1;
        string res = s.substr(0, 1);
        for(int i = 0; i <len-1;i++)
        {
            string s_odd = centerSpread(s,i,i);
            string s_even = centerSpread(s,i,i+1);
            string maxLenStr = s_odd.size() > s_even.size() ? s_odd : s_even;
            if (maxLenStr.length() > maxLen) {
                maxLen = maxLenStr.size();
                res = maxLenStr;
            }
        }
        return res;
    }
    string centerSpread(string s, int left, int right)
    {
        int i = left;
        int j =right;
        while(i>=0 & j<s.size())
        {
            if(s[i]==s[j])
            {
                i--;
                j++;
            }
            else
                break;
        }
        return s.substr(i+1, j-i-1);
    }
};
Published 85 original articles · won praise 0 · Views 379

Guess you like

Origin blog.csdn.net/weixin_38312163/article/details/105003198
Recommended