码迷,mamicode.com
首页 > 其他好文 > 详细

214. Shortest Palindrome

时间:2019-04-15 19:54:13      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:length   ems   div   detail   bre   tput   arc   http   sea   

Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.

 

Example 1:

Input: "aacecaaa"
Output: "aaacecaaa"

Example 2:

Input: "abcd"
Output: "dcbabcd"

 

Approach #1: Brute Force. [Java] [364 ms]

class Solution {
    public String shortestPalindrome(String s) {
        int n = s.length();
        if (n < 2) return s;
        for (int i = n-2; i >= 0; --i) {
            if (checkEven(i, s)) {
                String substr = s.substring(2*i+2);
                for (int j = 0; j < substr.length(); ++j) 
                    s = substr.charAt(j) + s;
                
                return s;
            } else if (checkOod(i, s)) {
                String substr = s.substring(2*i+1);
                for (int j = 0; j < substr.length(); ++j) 
                    s = substr.charAt(j) + s;
                
                return s;
            }
        }
        return s;
    }
    
    boolean checkEven(int idx, String s) {
        int l = idx, r = idx + 1;
        while (l >= 0 && r < s.length()) {
            if (s.charAt(l) == s.charAt(r)) {
                l--;
                r++;
            } else {
                break;
            }
        }
        if (l == -1 && r <= s.length()) return true;
        else return false;
    }
    
    boolean checkOod(int idx, String s) {
        int l = idx, r = idx;
        while (l >= 0 && r < s.length()) {
            if (s.charAt(l) == s.charAt(r)) {
                l--;
                r++;
            } else {
                break;
            }
        }
        if (l == -1 && r <= s.length()) return true;
        else return false;
    }
}

  

Approach #2: KMP. [Java] [3ms]

class Solution {
    
    public String shortestPalindrome(String s) {
        String temp = s + "#" + new StringBuilder(s).reverse().toString();
        int[] table = getTable(temp);
        
        return new StringBuilder(s.substring(table[table.length-1])).reverse().toString() + s;
    }
    
    public int[] getTable(String s) {
        int[] table = new int[s.length()];
        int len = 0, i = 1;
        table[0] = 0;
        
        while (i < s.length()) {
            if (s.charAt(i) == s.charAt(len)) {
                len++;
                table[i] = len;
                i++;
            } else {
                if (len != 0) {
                    len = table[len-1];
                } else {
                    table[i] = len;
                    i++;
                }
            }
        }
        
        
        return table;
    }
}

  

  

Reference:

https://www.geeksforgeeks.org/java-program-for-kmp-algorithm-for-pattern-searching-2/

https://leetcode.com/problems/shortest-palindrome/discuss/60113/Clean-KMP-solution-with-super-detailed-explanation

 

214. Shortest Palindrome

标签:length   ems   div   detail   bre   tput   arc   http   sea   

原文地址:https://www.cnblogs.com/ruruozhenhao/p/10712465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!