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 ...
随机推荐
- iOS中--NSArray调用方法详解 (李洪强)
下面的例子以 NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"to ...
- OpenCV count the number of connected camera 检测连接的摄像头的数量
有时候在项目中我们需要检测当前连接在机子上的摄像头的数量,可以通过下面的代码实现,其中连接摄像头的最大数量maxCamNum可以任意修改: /** * Count current camera num ...
- 发生了COMException 异常来自 HRESULT:0x80040228
异常信息: 发生了COMException 异常来自 HRESULT:0x80040228 原因解决方法:窗体中忘记放LicenseControl控件.,加上LicenseControl即可
- 浅谈Java中的Set、List、Map的区别
http://developer.51cto.com/art/201309/410205_all.htm
- php中的正则表达式具体的说明案例
让我们看看两个非凡的字符:'^' 和 '$' 他们是分别用来匹配字符串的开始和结束,一下分别举例说明: "^The": 匹配以 "The"开头的字符串; &qu ...
- Servlet 编程 简单流程处理(重定向)
流程(来自jiekexueyuan) servlet sucess.jsp error.asp同理 运行结果:
- SAE saestorage.class.php文件的封装代码
Source for file saestorage.class.php Documentation is available at saestorage.class.php <?php /** ...
- Java学习资源
Java技术路线图 指路明灯 一位资深程序员大牛给予Java初学者的学习路线建议 Java源码阅读的真实体会 概要 JDK发展历程 Java项目经验 基于java平台的常用资源 官方文档 Java™ ...
- PHP 设计模式 笔记与总结(5)PHP 魔术方法的使用
PHP 魔术方法的使用 ① __get/__set:将对象的属性进行接管 当访问一个不存在的对象属性时: index.php <?php define('BASEDIR',__DIR__); / ...
- pycharm使用笔记
Basic code completion (the name of any class, method or variable) control + 空格 # 代码补全,如果跟系统spotligh ...