Sunday, February 15, 2015

LeetCode [5] Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
class Solution {
public:
    string longestPalindrome(string s) {
        int n = s.size();
        string ret;
        vector<vector<int>> dp(n, vector<int>(n, 0));
        for(int i=n-1; i>=0; --i){
            for(int j=i; j<n; ++j){
                if(i!=j){
                    if(s[i]==s[j]){
                        if(i+1==j) dp[i][j] = 2;
                        else if(dp[i+1][j-1]>0) dp[i][j]=dp[i+1][j-1]+2;
                    }
                }else{
                    dp[i][j]=1;
                }
                
                if(ret.size()<dp[i][j]) ret = s.substr(i, dp[i][j]);
            }
        }
        return ret;
    }
};

class Solution {
public:
    string longestPalindrome(string s) {
        int start, len = 0, n = s.size();
        bool dp[n][n];
        memset(dp, false, n*n*sizeof(bool));
        const char *d = s.data();
        
        for(int i=0; i<n; ++i){
            dp[i][i] = true;
            if(len<1){start = i; len = 1;}
        }
        
        for(int i=0; i<n-1; ++i){
            if(d[i]==d[i+1]){
                dp[i][i+1] = true;
                if(len<2){start = i; len = 2;}
            }
        }
        
        for(int i=n-3; i>=0; --i){
            for(int j=i+2; j<n; ++j){
                if(d[i]==d[j] && dp[i+1][j-1]){
                    dp[i][j] = true;
                    if(len<j-i+1){len = j-i+1; start = i;}
                }
            }
        }
        
        return s.substr(start, len);
    }
};

class Solution {
public:
    string longestPalindrome(string s) {
        bool dp[1000][1000] = {false};
        int n = s.size();
        string ret;
        for(int i=n-1; i>=0; --i){
            for(int j=i; j<n; ++j){
                if(s[i]==s[j]){
                    if(i+1>j-1 || dp[i+1][j-1]){
                        dp[i][j] = true;
                        if(j-i+1>(int)ret.size()){
                            ret = s.substr(i, j-i+1);
                        }
                    }
                }
            }
        }
        return ret;
    }
};

class Solution {
    int expand(int &l, int &r, string s){
        int len = -1;
        int n = s.size();
        while(l>=0 && r<n && s[l]==s[r]){
            len = r-l+1;
            l--;
            r++;
        }
        l++;
        r--;
        return len;
    }
public:
    string longestPalindrome(string s) {
        int n = s.size();
        if(n<=1) return s;
        int start = 0, len = 1;
        for(int i=0; i<n; ++i){
            int l = i;
            int r = i;
            int len1 = expand(l, r, s);
            if(len1>len){
                start = l;
                len = len1;
            }
            l = i;
            r = i+1;
            len1 = expand(l, r, s);
            if(len1>len){
                start = l;
                len = len1;
            }            
        }
        return s.substr(start, len);
    }
};

class Solution {
public:
    string expanding(string s){
        string T;
        for(int i=0; i<s.size(); ++i){
            T += '#';
            T += s[i];
        }
        T += "#";
        return T;
    }

    string longestPalindrome(string s) {
        string T = expanding(s);
        int n = T.size();
        int dp[n];
        memset(dp, 0, n*sizeof(int));

        int C = 0, R = 0, i = 1, maxLen = 0, start = 0;
        for(int i=1; i<n; ++i){
            int i_mirror = C*2-i;
            dp[i] = (R>i)?min(R-i, dp[i_mirror]):0;
            while(i-dp[i]-1>=0 && i+dp[i]+1<n && T[i-dp[i]-1]==T[i+dp[i]+1]){
                dp[i]++;
            }
            if(i+dp[i]>R){
                C = i;
                R = i+dp[i];
            }
            if(dp[i]>maxLen){
                maxLen = dp[i];
                start = (i-maxLen)/2;
            }
       }
        return s.substr(start, maxLen);
    }
};

No comments:

Post a Comment