原题链接在这里:https://leetcode.com/problems/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.

For example:

Given "aacecaaa", return "aaacecaaa".

Given "abcd", return "dcbabcd".

题解:

求s前缀和 reverse s 后缀最长重合.

Time Complexity: O(n ^ 2).

Space: O(n).

AC Java:

 class Solution {
public String shortestPalindrome(String s) {
if(s == null || s.length() == 0){
return s;
} int n = s.length();
String rev = new StringBuilder(s).reverse().toString();
for(int i = 0; i < n; i++){
if(s.substring(0, n - i).equals(rev.substring(i))){
return rev.substring(0, i) + s;
}
} return rev + s;
}
}
其实求最短回文串,其实可以看作两个字符串求两串中的最长匹配字符,比如 串 “ abcd”

  • 注意,由于这里一个串可以是回文串,所以此处的前缀和后缀应该分别加上最后一个和第一个字符
就是求“abcd”和反串”dcba“的前缀和后缀最大匹配长度

原始串 前缀 反转串 后缀 最大匹配
abcd a ab abc abcd dcba a ba cba dcba a

由上面可以看出,abcd和dcba的最长匹配为a,一个字符,那么最后的回文串就是 反转串的长度4减去匹配长度1,得到3, 即反转串的前三个字符加上 原始串组成 ”abcabcd“

KMP算法中曾今和相似的利用过最大前缀和后缀求next[]数组,如果我们这样看 将原始串S和反转串R形成一个新串New

S+#+反转   =  abcd#dcba 

Note: 这里之所以要加上#是为了防止p[New.length()-1]的值要大于s.length().

另外需要注意如下例子,产生了aabba, 如果不加"#", 前面和反转相加正好产生了一个很长的palindrome.

Input:"aabba"
Output:"aabba"
Expected:"abbaabba"
另外最后要加空格是因为next 把标记右移了一位,加空格是为了让这一位显现出来。
Time Complexity: O(n). Space: O(n).

AC Java:

 public class Solution {
public String shortestPalindrome(String s) {
if(s == null || s.length() <=1){
return s;
}
StringBuilder rev = new StringBuilder(s);
//add "#" 为了防止next后面的数字大于 s的长度,并且s本身能和后面产生palindrome, 如"aabba"
String mirror = s + "#" + rev.reverse().toString() + " ";
int [] next = new int[mirror.length()];
getNext(mirror,next);
StringBuilder res = new StringBuilder(s.substring(next[next.length-1]));
return res.reverse().toString() + s;
}
private void getNext(String s, int [] next){
next[0] = -1;
int k = -1;
int j = 0;
while(j<s.length()-1){
if(k==-1 || s.charAt(k) == s.charAt(j)){
k++;
j++;
next[j] = k;
}else{
k=next[k];
}
}
}
}

类似Implement strStr().

参考了这篇帖子:http://blog.csdn.net/yujin753/article/details/47047155

LeetCode Shortest Palindrome的更多相关文章

  1. [LeetCode] 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

    题目: Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...

  3. Java for LeetCode 214 Shortest Palindrome

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

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

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

  5. 【LeetCode】214. Shortest Palindrome 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀是否回文 判断前缀 相似题目 参考资料 日期 题 ...

  6. LeetCode 214 Shortest Palindrome

    214-Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...

  7. 【leetcode】Shortest Palindrome(hard)★

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

  8. 【Leetcode】Shortest Palindrome

    Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...

  9. 【LeetCode】214. Shortest Palindrome

    Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...

随机推荐

  1. C#页面添加提交数据后跳出小弹窗的功能

    很简单,将小弹窗部分写进一个div,利用div的visible属性去控制是否显示,首先默认为false; 当后台程序执行到插入数据完成后,设置session状态值为‘yes’ 判断,当session状 ...

  2. 查看GCC编译C的中间代码

    小测试,记录一下命令 XD 一.C程序源码(code.c): 二.中间代码 由 gcc -fdump-tree-all code.c 得到 查看 code.c.012t.cfg 三.汇编级代码 (64 ...

  3. iOS真机调试的验证项

    基本的匹配项 App 描述文件 BundleID (匹配) ApplicationID Cert (匹配) Cert DeviceID (被包含) DeviceIDs 描述文件就是包含了各种需要验证信 ...

  4. 描述了say_hello函数的具体内容,调用zend_printf系统函数在php中打印字符串

    下载一个php的源代码包,这里使用的是php 4.0.5版,解压后会看到php的根目录下会有README.EXT_SKEL这样一个文件,打开详细阅读了一下,发现了一个非常好用的工具,这个工具可以帮你构 ...

  5. RAID 容量计算器

    https://www.synology.com/zh-cn/support/RAID_calculator   磁盘阵列比较表   n/2 n/2 n n/2 安全性高 综合RAID 0/1优点,理 ...

  6. OpenCV学习笔记——形态学梯度操作

    代码: #include<cv.h> #include<highgui.h> int main(void) { cvNamedWindow("cmp"); ...

  7. Java学习记录-注解

    注解 一.org.springframework.web.bind.annotation ControllerAdviceCookieValue : 可以把Request header中关于cooki ...

  8. JS页面跳转 神器! window.href window.open

    window.open 打开页面, 网址为新的网址 window.href 为打开本网址的链接, 为网站URL+传入的URL

  9. VS常见错误

    error C2572: “****”: 重定义默认参数 : 参数 默认参数只需在声明原型时使用,定义的时候就不需要. error C2572

  10. Visual Studio开发环境最佳字体及配色

    环境: Visual Studio 2010,(本人使用的windows 7) 字体:Fixedsys, 12pt,下载地址:http://www.fixedsysexcelsior.com 普通文本 ...