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 ...
随机推荐
- jsp页面中的代码执行加载顺序介绍
1. java是在服务器端运行的代码,jsp在服务器的servlet里运行,而javascript和html都是在浏览器端运行的代码.所以加载执行顺序是是java>jsp>js. 2. j ...
- lightning mdb 源代码分析(2)
本系列前一篇已经分析了lightningmdb的整体架构和主要的数据结构.本文将介绍一下MMAP原理以及lmdb中如何使用它. 1. Memory Map原理 内存映射文件与虚拟内存有些类似,通过内存 ...
- MySQL数据库管理常用命令
参考: http://blog.linuxeye.com/419.html 安装 利用RPM包安装MySQL 设置TCP 3306端口的iptables root密码管理 设置root用户 ...
- 分布式架构高可用架构篇_04_Keepalived+Nginx实现高可用Web负载均衡
参考: 龙果学院http://www.roncoo.com/share.html?hamc=hLPG8QsaaWVOl2Z76wpJHp3JBbZZF%2Bywm5vEfPp9LbLkAjAnB%2B ...
- javaWeb应用部署结构浅析
要成功部署一个Web应用,则必须遵循以下标准(参考)目录结构. 2.目录说明 1)WEB-INF目录:必须直接放在Web应用上下文之下(即一级目录). 2)class目录:必须直接放在WEB-INF目 ...
- emacs notepad notepad++ 撤销比较
以前使用编辑器都是直接上手,未读过什么的文档了解.所谓撤销只是使用,也不了解究竟撤销到何处,阅读了emacs的文档才知道有许多区别呢. 输入this is a pen,然后一个个字符地删除到this ...
- 《Pro Git》笔记1:起步
第一章 起步 1.关于版本控制 版本控制用于记录和追踪目录结构和文件内容变化,能够追溯过去的任何修改和变化,并恢复到任何历史状态. 版本控制系统可以按照发展过程分成以下几类: 目录备份.记录版本变化最 ...
- MyBatis传入参数为集合 list 数组 map写法
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...
- git 使用钩子直接推送到工作目录
远端机器 $ mkdir /www/teacherapi # 创建工作目录 $ cd /data/git $ git init teacherapi.git --bare --shared Init ...
- 为什么我的联想打印机M7450F换完墨粉之后打印机显示请更换墨粉盒?这是我的墨盒第一次灌粉·、
需要打印机清零,可以网上查到的,要不就去买颗芯片换上关掉机器 →开机的同时按住功能按扭不松手开机→进入维修模式→翻到84功能项→按OK→用下翻键找到PROCESS CHECK→按OK 按扭→关机→正常 ...