LeetCode Shortest Palindrome
原题链接在这里: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 | a ab abc abcd | dcba | a ba cba dcba | a |
由上面可以看出,abcd和dcba的最长匹配为a,一个字符,那么最后的回文串就是 反转串的长度4减去匹配长度1,得到3, 即反转串的前三个字符加上 原始串组成 ”abcabcd“
S+#+反转 = abcd#dcba
Note: 这里之所以要加上#是为了防止p[New.length()-1]的值要大于s.length().
另外需要注意如下例子,产生了aabba, 如果不加"#", 前面和反转相加正好产生了一个很长的palindrome.
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];
}
}
}
}
参考了这篇帖子:http://blog.csdn.net/yujin753/article/details/47047155
LeetCode Shortest Palindrome的更多相关文章
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- 【回文】leetcode - Shortest Palindrome
题目: Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...
- 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. ...
- [LeetCode] 214. Shortest Palindrome 最短回文串
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- 【LeetCode】214. Shortest Palindrome 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀是否回文 判断前缀 相似题目 参考资料 日期 题 ...
- LeetCode 214 Shortest Palindrome
214-Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...
- 【leetcode】Shortest Palindrome(hard)★
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- 【Leetcode】Shortest Palindrome
Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...
- 【LeetCode】214. Shortest Palindrome
Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...
随机推荐
- CSS中zoom:1的作用 ,小标签大作用
CSS中zoom:1的作用兼容IE6.IE7.IE8浏览器,经常会遇到一些问题,可以使用zoom:1来解决,有如下作用:触发IE浏览器的haslayout解决ie下的浮动,margin重叠等一些问题. ...
- cvReleaseImage 释放内存出错
cvReleaseImage是OpenCV中C语言库中的释放图片内存的函数,比如我们加载或者克隆了一幅图片,当不需要这幅图片了时,我们为了避免内存泄露,需要释放这些空间,可以参见我之前的博客OpenC ...
- https资料
1.HTTPS的七个误解 http://blog.httpwatch.com/2011/01/28/top-7-myths-about-https/ 中文 http://www.cnblogs.c ...
- data structure | heap
#include <iostream> #include <string.h> using namespace std; template <class T> cl ...
- Javascript 笔记与总结(2-10)删除节点,创建节点
[删除节点] 步骤: ① 找到对象 ② 找到他的父对象 parentObj ③ parentObj.removeChild(子对象); [例] <!DOCTYPE html> <ht ...
- ecshop 完美解决动态ip登录超时和购物车清空问题
ecshop 完美解决动态ip登录超时和购物车清空问题 ECSHOP模板/ecshop开发中心(www.68ecshop.com) / 2014-05-06 前一段时间,ECSHOP开发中心的一个客户 ...
- HTML: css 修飾文本和字體
因爲這個我認爲不用記,所以關於css 修飾文本&字體的屬性只需要打開css手冊,找到(屬性 > 文本) & (屬性 > 字體)翻看即可. 關於字體屬性: Propertie ...
- word 使用宏批量设置表格
Sub ChangeTable() Application.Browser.Target = wdBrowseTable To ActiveDocument.Tables.Count ActiveDo ...
- linux面试题及答案
http://www.cnblogs.com/itech/archive/2011/02/12/1952857.html 一.填空题:1. 在Linux系统中,以 文件 方式访问设备 .2. Linu ...
- 专家来了-提测-改bug-上线10号
集成那天,同事帮忙改了三个bug, 适配ios6约束,方法被调用两次, 郑晓杨吃饭,好像还欠我钱呢 Product-archive 打包 ------------------------------ ...