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.(前缀不能到最后一 ...
随机推荐
- Android使用am命令实现拨打电话、打开应用
前提: 在Android 通话自己主动化測试中会用到am命令去拨打电话.打开音乐播放器播放音乐等等操作. 这里总结一下am命令. Android am命令: (1)命令參数: am start -n ...
- Robot Framework操作
Robot Framework 介绍 RobotFramework是一款基于python的开源自动化测试框架,遵守Apache License 2.0协议,在此协议下所有人都可以免费开发和使用.因为R ...
- [JS进阶] HTML5 之文件操作(file)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/oscar999/article/details/37499743 前言 在 HTML 文档中 < ...
- mac下Android Studio干净卸载
1.卸载Android Studio,在终端(terminal)执行以下命令: rm -Rf /Applications/Android\ Studio.app rm -Rf ~/Library/Pr ...
- 从远程Linux Copy文件到本机 界面化操作
1.安装SSHSecureShellClient 2.打开 3.设置1,然后打开2就可以操作了
- Silverlight数据绑定之DataGrid
Silverlight数据绑定之DataGrid 时间:2011-08-03 01:59来源:网易博客 作者:Wilson. 点击:次 注:所有代码以C#为例 DataGrid绑定的数据对象: 1.D ...
- 【183】VMware + CentOS 安装教程等
目录: VMware 虚拟机安装 虚拟机上安装 CentOS VMware Tools 安装 为程序增加环境变量 其他 1. VMware 虚拟机安装 ※ 参考:VMware 虚拟机安装使用教程( ...
- 表单中的readOnly 和disabled
readonly和Disabled是用在表单中的两个属性,它们都能够做到使用户不能够更改表单域中的内容.但是它们之间有着微小的差别,总结如下: disabled也可以禁用按钮和链接: <butt ...
- 开源可扩展的Web视频播放器:Clappr Player
http://www.open-open.com/lib/view/open1417057033846.html http://www.csdn.net/article/2014-11-27/2822 ...
- 值得网页设计师&前端收藏的实用工具列表
原文地址:http://www.uisdc.com/tool-list-web-developers# 无论你是经验丰富的前端,还是刚刚起步的设计师,这些为真正的网页设计师和开发者所准备的实用工具.在 ...