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 ...
随机推荐
- QMessageBox 使用方法
在Qt中经常需要弹出窗口,QMessageBox可以实现此功能,一共有三种窗口,information, question, 和 warning,critical, about分别对应感叹号,问号和叉 ...
- ThinkPHP 中M方法和D方法详解----转载
转载的地址,http://blog.163.com/litianyichuanqi@126/blog/static/115979441201223043452383/ 自己学到这里的时候,不能清除的分 ...
- Scrum会议8(Beta版本)
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- CSS系列:less备忘
less备忘 //这是一个运行在koala中的less文件,//注释不会被编译到css文件中,/**/注释会 ****************by 李可 2016/04/19 /*所有,所有伪类*/ ...
- twitter storm源码走读之4 -- worker进程中线程的分类及用途
欢迎转载,转载请注明出版,徽沪一郎. 本文重点分析storm的worker进程在正常启动之后有哪些类型的线程,针对每种类型的线程,剖析其用途及消息的接收与发送流程. 概述 worker进程启动过程中最 ...
- Memcached 笔记与总结(5)Memcached 的普通哈希分布和一致性哈希分布
普通 Hash 分布算法的 PHP 实现 首先假设有 2 台服务器:127.0.0.1:11211 和 192.168.186.129:11211 当存储的 key 经过对 2 (2 台服务器)取模运 ...
- Eclipse中用User Library管理jar包
目的:为了更方便的管理jar包,而不是一股脑儿的将引用的jar包全部放在Web App Library下. 管理和配置: 第一步:管理 新建Library并引入项目中 右键项目->Build P ...
- Keil的lib生成个人总结
1. 生成lib 的工程可以没有main函数,可以只有一个.c文件,一个.c文件中可以只有一个函数 2. 需要在lib工程中建立一个.h文件, 必须用extern声明各全局变量和函数. 3. 调用li ...
- PROTEL99SE封装说明
PROTEL99SE封装说明 路板上的焊盘间的距离也就是300mil(因为在电机领域里,是以英制单位为主的.同样的, 对于无极性的电容,RAD0.1-RAD0.4 也是一样:对有极性的电容如电解电容 ...
- SET ? DECLARE
http://dev.mysql.com/doc/refman/5.7/en/declare-local-variable.html http://dev.mysql.com/doc/refman/5 ...