214. Shortest Palindrome

时间:2019-04-15
本文章向大家介绍214. Shortest Palindrome,主要包括214. Shortest Palindrome使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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

 

上一页 下一页

原文地址:http://www.manongjc.com/article/103520.html