3.5 Longest Palindromic Substring

来源:互联网 发布:unity3d 联网游戏视频 编辑:程序博客网 时间:2024/04/29 03:25

方法 I: Naive, Time O(n_3). Time Limit Exceed

public class Solution {    public String longestPalindrome(String s) {        //approach I: Brute-force        //N3        int maxLength = 0;        String result = null;        for(int i = 0; i < s.length(); i++){            for(int j = i+1; j < s.length(); j++){                if(checkPalindrome(s.substring(i, j))){//the answer use j+1 not j, why?                    if(j - i + 1 > maxLength){                        maxLength = j - i + 1;                        result = s.substring(i, j);                    }                }            }        }         return result;    }        private boolean checkPalindrome(String sub){        if(sub.length() == 1) return true;        int i = 0;        int j = sub.length()-1;        while(i < j){            if(sub.charAt(i) != sub.charAt(j)){                return false;            }            i++;            j--;        }        return true;    }}

方法II: DP. Time O(n_2), Space O(n_2). Time Limit Exceed

public class Solution {    //approach II: DP    public String longestPalindrome(String s) {        int length = s.length();        int [][] table = new int[length][length];        int maxLength = 0;        String longestStr = null;        //Initialization        //every single char is a palindromic string        for(int i = 0; i < table.length; i++){            table[i][i] = 1;        }        //if two consecutive chars are the same, they are a palindromic string        for(int i = 0; i < table.length - 1; i++){            if(s.charAt(i) == s.charAt(i+1)){                table[i][i+1] = 1;                maxLength = 2;                longestStr = s.substring(i, i+2);            }        }        //for all the other entries in the table         for(int l = 3; l <= table.length; l++){//l = length of the substring            for(int i = 0; i <=table.length - l; i++){                int j = i + l - 1;                if(s.charAt(i) == s.charAt(j)){                    table[i][j] = table[i+1][j-1];                    if(table[i][j] == 1 && l > maxLength){                        maxLength = l;                        longestStr = s.substring(i, j+1);                    }                }                else{                    table[i][j] = 0;                }            }        }        return longestStr;    }}

方法III:以每个字符(或邻近两个字符)为中间元素,扩展至左右两边。Time O(n_2), Space O(1).

public class Solution {    //approach II: Simple Approach. Time O(n_2), Space O(n)    //Use each char as the centering elements, and check its left and right for palindrome    public String longestPalindrome(String s) {        //if(s == null) return null;        if(s.length() <=1) return s;        String longest = s.substring(0, 1);                //check each substring centering at single letter i        for(int i = 0; i < s.length(); i++){            String tmp = helper(s, i, i);            if(tmp.length() > longest.length()){                longest = tmp;            }        }        //check each substring centering at single substring (i, i+1)        for(int i = 0; i < s.length(); i++){            String tmp = helper(s, i, i+1);            if(tmp.length() > longest.length()){                longest = tmp;            }        }        return longest;    }        private String helper(String s, int begin, int end){        while(begin >=0 && end <= s.length()-1 && s.charAt(begin) == s.charAt(end)){            begin --;            end ++;        }        return s.substring(begin+1, end);    }}

方法IV: Manachar's algorithm. 没看懂。


0 0