原题链接在这里: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:

  1. class Solution {
  2. public String shortestPalindrome(String s) {
  3. if(s == null || s.length() == 0){
  4. return s;
  5. }
  6.  
  7. int n = s.length();
  8. String rev = new StringBuilder(s).reverse().toString();
  9. for(int i = 0; i < n; i++){
  10. if(s.substring(0, n - i).equals(rev.substring(i))){
  11. return rev.substring(0, i) + s;
  12. }
  13. }
  14.  
  15. return rev + s;
  16. }
  17. }
其实求最短回文串,其实可以看作两个字符串求两串中的最长匹配字符,比如 串 “ 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

  1. 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:

  1. public class Solution {
  2. public String shortestPalindrome(String s) {
  3. if(s == null || s.length() <=1){
  4. return s;
  5. }
  6. StringBuilder rev = new StringBuilder(s);
  7. //add "#" 为了防止next后面的数字大于 s的长度,并且s本身能和后面产生palindrome, 如"aabba"
  8. String mirror = s + "#" + rev.reverse().toString() + " ";
  9. int [] next = new int[mirror.length()];
  10. getNext(mirror,next);
  11. StringBuilder res = new StringBuilder(s.substring(next[next.length-1]));
  12. return res.reverse().toString() + s;
  13. }
  14. private void getNext(String s, int [] next){
  15. next[0] = -1;
  16. int k = -1;
  17. int j = 0;
  18. while(j<s.length()-1){
  19. if(k==-1 || s.charAt(k) == s.charAt(j)){
  20. k++;
  21. j++;
  22. next[j] = k;
  23. }else{
  24. k=next[k];
  25. }
  26. }
  27. }
  28. }

类似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. ASP.NET后台JS弹框使前台页面样式丢失 解决办法

    Response.Write("<script>alert('您还没有上传相关图片!');</script>");是向前台输出js 应该用下面的方法 Cli ...

  2. zabbix配置文件详解

    Zabbix之配置文件详解   zabbix配置文件种类: zabbix_server配置文件zabbix_server.conf zabbix_proxy配置文件zabbix_proxy.conf ...

  3. pajax

    pjax网址:https://libraries.io/bower/yii2-pjax 1. 连接指定的div,实行pjax ,利用 linkSelector 方法<div id="c ...

  4. Angular:手动脏检查/$apply/$digest和监控对象/$watch

    声明:借鉴好多chm资料.视频.PDF总结如下: 一.$apply的引入 View <div ng-app=""> <div ng-controller=&quo ...

  5. 让你的PHP更安全之PHP.ini

    让你的PHP更安全之PHP.ini 发布时间:2013-05-02 12:43:06   来源:PHP100论坛   评论:0 点击: 次 [字号:大 中 小] QQ空间新浪微博腾讯微博人人网豆瓣网百 ...

  6. PHP文件操作 之往一个文件写入数据

    //打开一个文件 $f = fopen($filename,'wb'); $filename:打开一个文件,不存在则自动创建,如果不能创建,说明指定的文件目录有错误 wb:写入的方式 ---- 覆盖原 ...

  7. 20145317彭垚 《Java程序设计》第五次实验报告

    20145317彭垚实验五 Java网络编程及安全 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.05.06 18:30-21: ...

  8. SVN使用安装

    SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...

  9. Natural Language Processing Computational Linguistics

    http://www.nltk.org/book/ch00.html After this, the pace picks up, and we move on to a series of chap ...

  10. 1Web语言:开始了解HTML

    HTML是hybertext markup language的缩写,用来告诉浏览器网页的结构和内容.HTML的所有工作都是关于结构的,而不是外观.CSS是级联样式表(Cascading Style S ...