给一个字符串 S, 你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
例如:
给出 "aacecaaa",返回 "aaacecaaa"。
给出 "abcd",返回 "dcbabcd"。

详见:https://leetcode.com/problems/shortest-palindrome/description/

Java实现:

暴力解法:

class Solution {
public String shortestPalindrome(String s) {
if (s.isEmpty()) {
return s;
}
int i = s.length() - 1;
while (i >= 0) {
String spre = s.substring(0, i + 1);
if (isPalindrome(spre)) {
break;
}
--i;
}
String pre = new StringBuilder(s.substring(i + 1)).reverse().toString();
return pre + s;
} private boolean isPalindrome(String s) {
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
}

KMP算法解法:

class Solution {
public String shortestPalindrome(String s) {
if (s.isEmpty()) {
return s;
}
String rs = new StringBuilder(s).reverse().toString();
String newStr = s + "#" + rs;
int[] next = new int[newStr.length()];
for (int i = 1; i < newStr.length(); ++i) {
int j = next[i - 1];
while (j > 0 && newStr.charAt(i) != newStr.charAt(j)) {
j = next[j - 1];
}
j += (newStr.charAt(i) == newStr.charAt(j)) ? 1 : 0;
next[i] = j;
}
return rs.substring(0, s.length() - next[newStr.length() - 1]) + s;
}
}

参考:https://www.cnblogs.com/ganganloveu/p/4621327.html

https://www.cnblogs.com/grandyang/p/4523624.html

https://leetcode.com/problems/shortest-palindrome/discuss/60141/C++-8-ms-KMP-based-O(n)-time-and-O(n)-memory-solution

214 Shortest Palindrome 最短回文串的更多相关文章

  1. [LeetCode] 214. Shortest Palindrome 最短回文串

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  2. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. [Swift]LeetCode214. 最短回文串 | Shortest Palindrome

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. Java实现 LeetCode 214 最短回文串

    214. 最短回文串 给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出 ...

  5. leetcode 214. 最短回文串 解题报告

    给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...

  6. Leetcode 214.最短回文串

    最短回文串 给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: &qu ...

  7. [python,2019-02-15] 最短回文串

    给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...

  8. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

  9. 算法进阶面试题01——KMP算法详解、输出含两次原子串的最短串、判断T1是否包含T2子树、Manacher算法详解、使字符串成为最短回文串

    1.KMP算法详解与应用 子序列:可以连续可以不连续. 子数组/串:要连续 暴力方法:逐个位置比对. KMP:让前面的,指导后面. 概念建设: d的最长前缀与最长后缀的匹配长度为3.(前缀不能到最后一 ...

随机推荐

  1. Deepin-还原Windows平台

    首次启动! 是不是感觉很迷茫呢? 找不到存在感 先设置成Windows那种高校模式(右键下面任意区域) OK了吧,然后我们找到“启动器”或者按Windows键(在Deepin linux我们称为Sup ...

  2. 汉诺塔 Tower of Hanoi

    假设柱子标为A,B.C.要由A搬至C,在仅仅有一个盘子时,就将它直接搬至C:当有两个盘子,就将B作为辅助柱.假设盘数超过2个.将第二个下面的盘子遮起来,就非常easy了.每次处理两个盘子,也就是:A- ...

  3. icvSetWeightsAndClasses

    /* *icvSetWeightsAndClasses *作用:给训练样本的权重和类别赋值 */ static void icvSetWeightsAndClasses( CvHaarTraining ...

  4. 通过GhostDoc实现自定义方法概要(summary)

    首先是下载GhostDoc 来自园友:http://www.cnblogs.com/VAllen/p/GhostDocPro49.html 修改模板 安装好后,修改下模板,工具>GhostDoc ...

  5. html2canvas 导出包含滚动条的内容

    import html2canvas from 'html2canvas'; exportPDF() { // 导出为 pdf let dom = document.querySelector('yo ...

  6. Node-webkit 介绍

    什么是Node-webkit ? Node-webkit 是Intelproject师rogerwang写的一个基于node.js和chromium的应用程序执行环境,通过node-webkit,我们 ...

  7. Spring Task 定时任务

    所谓定时任务.就是依据我们设定的时间定时运行任务,就像定时发邮件一样,设定时间到了.邮件就会自己主动发送. 在Spring大行其道的今天,Spring也提供了其定时任务功能,Spring Task.同 ...

  8. JAVA学习(七):方法重载与方法重写、thiskeyword和superkeyword

    方法重载与方法重写.thiskeyword和superkeyword 1.方法重载 重载可以使具有同样名称但不同数目和类型參数的类传递给方法. 注: 一是重载方法的參数列表必须与被重载的方法不同,而且 ...

  9. Semantic Parsing(语义分析) Knowledge base(知识图谱) 对用户的问题进行语义理解 信息检索方法

    简单说一下所谓Knowledge base(知识图谱)有两条路走,一条是对用户的问题进行语义理解,一般用Semantic Parsing(语义分析),语义分析有很多种,比如有用CCG.DCS,也有用机 ...

  10. Tju 4119. HDFS

    4119.   HDFS Time Limit: 5.0 Seconds   Memory Limit: 5000KTotal Runs: 225   Accepted Runs: 77 In HDF ...