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".
分析:
就是找出从原字符串中从第一个字符开始能够组成的最长palindome. 然后把原字符串中后面部分reverse后放在前面,这样整个字符串就是palindrome, 并且长度最小。
public class Solution {
public String shortestPalindrome(String s) {
if (s == null || s.length() <= )
return s;
int size = ;
for (int i = ; i <= (s.length() - ) / ; i++) {
size = Math.max(size, maxSize(s, i, i));
size = Math.max(size, maxSize(s, i, i + ));
}
String str = s.substring(size);
StringBuilder sb = new StringBuilder(str);
sb.reverse();
return sb.toString() + s;
}
private int maxSize(String s, int left, int right) {
while (left >= && right < s.length() && s.charAt(left) == s.charAt(right)) {
left--;
right++;
}
if (left == -) {
return right - left - ;
}
return ;
}
}
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 214 Shortest Palindrome
214-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】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
原题链接在这里:https://leetcode.com/problems/shortest-palindrome/ 题目: Given a string S, you are allowed to ...
- 214. Shortest Palindrome
题目: Given a string S, you are allowed to convert it to a palindrome by adding characters in front of ...
- 【Leetcode】Shortest Palindrome
Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...
- 【回文】leetcode - Shortest Palindrome
题目: Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...
- [Swift]LeetCode214. 最短回文串 | Shortest Palindrome
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- ServletContext
1.为什么需要servletContext 需求1 需求2 --------------->解决之道servletContext servletContext 1.ServletC ...
- bootstrap和bootstrap-select的outline设置
.btn:focus, .btn:active:focus, .btn.active:focus, .btn.focus, .btn:active.focus, .btn.active.focus { ...
- java正则表达式 --简单认识
学习目标 正则表达式的作用正则表达式的模式匹配Pattern类和Matcher类的使用掌握String对正则的支持具体内容一.认识正则(为什么要有正则) 方便的对数据进行匹配 执行复杂的字符串验证.拆 ...
- gradle init.gradle的文件配置 使用
init.gradle文件在build开始之前执行,所以你可以在这个文件配置一些你想预先加载的操作例如配置build日志输出.配置你的机器信息,比如jdk安装目录,配置在build时必须个人信息,比如 ...
- zookeeper原理(转)
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等.Zookeeper是hadoop的一个子项目,其 ...
- [Mongodb]删除指定字段
摘要 如果想要删除mongodb中一个document的某个字段,该如何做呢? 方法模版 db.user.update({"email_state":{"$exists& ...
- linq和lanmbda表达式比较解析
- select2
.select2-container .select2-choice { height: 34px; line-height: 34px; } .自定义 组件高度 在css 里面设置 .select2 ...
- Spark之scala
一.什么是scala scala 是基于JVMde 编程语言.JAVA是运行在jvm上的编程语言,java 源代码通过jvm被编译成class 文件,然后在os上运行class 文件.scala是运行 ...
- C#之interface接口
C#中接口与抽象类很相似,他们都无法实例化自己的对象,但是他们也有很重要的区别.Interface与Abstract class中,类不能多重继承,但是接口可以多重继承. 这段代码表明,声明接口的方法 ...