zoukankan      html  css  js  c++  java
  • 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

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    oracle之数据限定与排序
    oracle之分组函数
    oracle之SQL的数据类型
    lftp简单使用
    黑盘-蓝盘-绿盘-红盘
    windows 路由
    http扩展请求头中的x-Forwarded-For
    http状态码304
    firewall 实现数据的端口转发
    通过curl获取网页访问时间
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10712465.html
Copyright © 2011-2022 走看看