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的更多相关文章

  1. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  2. LeetCode 214 Shortest Palindrome

    214-Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...

  3. 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. ...

  4. 【leetcode】Shortest Palindrome(hard)★

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. LeetCode Shortest Palindrome

    原题链接在这里:https://leetcode.com/problems/shortest-palindrome/ 题目: Given a string S, you are allowed to ...

  6. 214. Shortest Palindrome

    题目: Given a string S, you are allowed to convert it to a palindrome by adding characters in front of ...

  7. 【Leetcode】Shortest Palindrome

    Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...

  8. 【回文】leetcode - Shortest Palindrome

    题目: Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...

  9. [Swift]LeetCode214. 最短回文串 | Shortest Palindrome

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

随机推荐

  1. C#根据时间产生有序的GUID编码

    public static Guid GenerateGuid() { byte[] guidArray = Guid.NewGuid().ToByteArray(); , , ); DateTime ...

  2. Make命令

    Make命令 一.Make的概念 Make这个词,英语的意思是"制作".Make命令直接用了这个意思,就是要做出某个文件.比如,要做出文件a.txt,就可以执行下面的命令. $ m ...

  3. WindowsService 创建.安装.部署

    windows服务的用法很适合用于一些长期跑的项目..不需要人工操作..不需要服务器一直登陆..很方便.. 不说废话..直接开整.. 启动VS2012..创建Windows服务项目.. 确定..创建成 ...

  4. text-indent:2em详解

    text-indent:2em; 解释一下:text的意思是文本,indent在计算机英语中意思是缩进,至于后面的2em意思就是2个相对单位: em又是什么单位? em这个单位的意思就是文字的高度,1 ...

  5. MYSQL调优

    4核8G内存配置文件 explain SQL 查看SQL索引使用情况. my.cnf skip-external-locking skip-name-resolve back_log= key_buf ...

  6. Nginx负载均衡配置实例详解(转)

    负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...

  7. 遍历JObject

    JObject jo = JObject.Parse(jsonStr); IEnumerable<JProperty> properties = jo.Properties(); fore ...

  8. php去掉字符串的最后一个字符附substr()的用法

    转自:http://www.jb51.net/article/26604.htm 今天项目中用到,去掉字符串中的最后一个字符 原字符串1,2,3,4,5,6, 去掉最后一个字符"," ...

  9. C/C++/Java/C#语言的基本类型

    C语言的基本类型有:char, short ,int ,long ,float ,double 一共6种基本类型. C++语言有:bool, char ,wchar_t, short, int , l ...

  10. Linux运维初级教程(一)Shell脚本

    序,掌握shell脚本是linux运维工程师的最基础技能. 一.脚本语言 与高级语言不通,脚本语言运行时需要调用相应的解释器来翻译脚本中的内容. 常见的脚本语言有Shell脚本.Python脚本.ph ...