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 ...
随机推荐
- ASP.NET后台JS弹框使前台页面样式丢失 解决办法
Response.Write("<script>alert('您还没有上传相关图片!');</script>");是向前台输出js 应该用下面的方法 Cli ...
- zabbix配置文件详解
Zabbix之配置文件详解 zabbix配置文件种类: zabbix_server配置文件zabbix_server.conf zabbix_proxy配置文件zabbix_proxy.conf ...
- pajax
pjax网址:https://libraries.io/bower/yii2-pjax 1. 连接指定的div,实行pjax ,利用 linkSelector 方法<div id="c ...
- Angular:手动脏检查/$apply/$digest和监控对象/$watch
声明:借鉴好多chm资料.视频.PDF总结如下: 一.$apply的引入 View <div ng-app=""> <div ng-controller=&quo ...
- 让你的PHP更安全之PHP.ini
让你的PHP更安全之PHP.ini 发布时间:2013-05-02 12:43:06 来源:PHP100论坛 评论:0 点击: 次 [字号:大 中 小] QQ空间新浪微博腾讯微博人人网豆瓣网百 ...
- PHP文件操作 之往一个文件写入数据
//打开一个文件 $f = fopen($filename,'wb'); $filename:打开一个文件,不存在则自动创建,如果不能创建,说明指定的文件目录有错误 wb:写入的方式 ---- 覆盖原 ...
- 20145317彭垚 《Java程序设计》第五次实验报告
20145317彭垚实验五 Java网络编程及安全 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.05.06 18:30-21: ...
- SVN使用安装
SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...
- 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 ...
- 1Web语言:开始了解HTML
HTML是hybertext markup language的缩写,用来告诉浏览器网页的结构和内容.HTML的所有工作都是关于结构的,而不是外观.CSS是级联样式表(Cascading Style S ...