214 Shortest Palindrome 最短回文串
给一个字符串 S, 你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
例如:
给出 "aacecaaa",返回 "aaacecaaa"。
给出 "abcd",返回 "dcbabcd"。
详见:https://leetcode.com/problems/shortest-palindrome/description/
Java实现:
暴力解法:
class Solution {
public String shortestPalindrome(String s) {
if (s.isEmpty()) {
return s;
}
int i = s.length() - 1;
while (i >= 0) {
String spre = s.substring(0, i + 1);
if (isPalindrome(spre)) {
break;
}
--i;
}
String pre = new StringBuilder(s.substring(i + 1)).reverse().toString();
return pre + s;
}
private boolean isPalindrome(String s) {
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
}
KMP算法解法:
class Solution {
public String shortestPalindrome(String s) {
if (s.isEmpty()) {
return s;
}
String rs = new StringBuilder(s).reverse().toString();
String newStr = s + "#" + rs;
int[] next = new int[newStr.length()];
for (int i = 1; i < newStr.length(); ++i) {
int j = next[i - 1];
while (j > 0 && newStr.charAt(i) != newStr.charAt(j)) {
j = next[j - 1];
}
j += (newStr.charAt(i) == newStr.charAt(j)) ? 1 : 0;
next[i] = j;
}
return rs.substring(0, s.length() - next[newStr.length() - 1]) + s;
}
}
参考:https://www.cnblogs.com/ganganloveu/p/4621327.html
https://www.cnblogs.com/grandyang/p/4523624.html
https://leetcode.com/problems/shortest-palindrome/discuss/60141/C++-8-ms-KMP-based-O(n)-time-and-O(n)-memory-solution
214 Shortest Palindrome 最短回文串的更多相关文章
- [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] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [Swift]LeetCode214. 最短回文串 | Shortest Palindrome
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java实现 LeetCode 214 最短回文串
214. 最短回文串 给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出 ...
- leetcode 214. 最短回文串 解题报告
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...
- Leetcode 214.最短回文串
最短回文串 给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: &qu ...
- [python,2019-02-15] 最短回文串
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
- 算法进阶面试题01——KMP算法详解、输出含两次原子串的最短串、判断T1是否包含T2子树、Manacher算法详解、使字符串成为最短回文串
1.KMP算法详解与应用 子序列:可以连续可以不连续. 子数组/串:要连续 暴力方法:逐个位置比对. KMP:让前面的,指导后面. 概念建设: d的最长前缀与最长后缀的匹配长度为3.(前缀不能到最后一 ...
随机推荐
- Visual Studio 2012 Fakes框架测试驱动开发TDD教程
一.前言 最近团队要尝试TDD(测试驱动开发)的实践,很多人习惯了先代码后测试的流程,对于TDD总心存恐惧,认为没有代码的情况下写测试代码时被架空了,没法写下来,其实,根据个人实践经验,TDD并不可怕 ...
- Django项目开发-小技巧
当你开发完一个Django项目之后肯定要吧他丢到服务器让跑起来,但是你在自己的环境下安装了好多的包,是不是在服务器中也要一个个的安装了, pip freeze > read.txt #这条命令会 ...
- 【bzoj2152】【聪聪可可】【点分治】
[问题描写叙述] 聪聪和可但是兄弟俩.他们俩常常为了一些琐事打起来,比如家中仅仅剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(但是他们家仅仅有一台电脑)--遇到这样的问题,普通情况下石头剪刀布就好 ...
- Xcode6 设置LaunchImage图标
最近设置LaunchImage图标时发现怎么都没有效果,后来发现是Xcode6中新建项目的时候会默认添加一个LaunchScreen.xib的文件,我们启动程序的时候也会发现,加载的时LaunchSc ...
- <s:property>的用法(jsp获取action中的值或者方法)
1,访问Action值栈中的普通属性: <s:property value="attrName"/> 2,访问Action值栈中的对象属性(要有get set方法) ...
- wget和curl
1 curl比wget支持更多的协议 2 wget是支持递归的,而curl不支持
- FineReport实现java报表统计图表的效果图
Java报表-ERP图表联动 Java报表-多维坐标轴图 Java报表-静态图表 Java报表-时间坐标轴 Java报表-图表报表动态交互 Java报表-图表热点链接 Java报表-图表缩放 Java ...
- B. Flag of Berland
B. Flag of Berland time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Android vector 标签 pathData 详解
转载地址:http://www.jianshu.com/p/a3cb1e23c2c4#rd Android Support Library 23.2 出来以后,在Android 5.0(API级别21 ...
- Ubuntu 搭建 LAMP 服务器
/******************************************************************** * Ubuntu 搭建 LAMP 服务器 * 说明: * 想 ...