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. ...
随机推荐
- XML简介
xml的简介(了解) * eXtensible Markup Language:可扩展标记型语言 ** 标记型语言:html是标记型语言 ...
- VFP笔记
1.计算圆的面积的计算器 650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/12/43/wKiom1MAsFzxm ...
- springmvc的form标签
1.要使用Spring MVC提供的表单标签,首先需要在视图页面添加: <%@ taglib prefix="form" uri="http://www.sprin ...
- Last-Modified、ETag、Expires和Cache-Control
前言 在客户端通过浏览器发出第一次请求某一个URL时,根据 HTTP 协议的规定,浏览器会向服务器传送报头(Http Request Header),服务器端响应同时记录相关属性标记(Http Rep ...
- 【8-15】Markdown语法学习
学习Markdown语法 来源简书URL #,支持六级标题 列表 用-或*(指无序列表),有序列表直接1. 2. 3. 这样,中间有空格,可乱序(-+*都可,不能混合使用,混合使用为嵌套) 这是一个无 ...
- 几种常用的JS类定义方法(转)
// 方法1 对象直接量 var obj1 = { v1 : "", get_v1 : function() { return this.v1; }, set_v1 : funct ...
- div+css使多行文字垂直居中?
1.单行文字: 设置height = line-height; 多行文字: 设置 padding, 自己要计算一下? vertical-align: 可以设置垂直居中, 但是只是针对本身就具有 ali ...
- PHP 5.5 新特性
文章转自:http://wulijun.github.io/2013/07/17/whats-new-in-php-5-5.html http://www.cnblogs.com/yjf512/p/3 ...
- [歪谈]我们该怎么正确面对"批评"
这两天看到网上有类似这样的话题:遇到批评你是如何面对? 其实标题中没有“领导”,并不是专指:遇到“领导”批评你是如何面对? 在IT界(其他行业和领域就不谈了). 批评分三个层面: 1. ...
- 由json字符串生成C#实体类的工具
json作为互联网上轻量便捷的数据传输格式,越来越受到重视.但在服务器端编程过程中,我们常常希望能通过智能提示来提高编码效率.JSON C# Class Generator 能将json格式所表示的J ...