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

解题思路:

本题最简单的思路是从后往前判断子串是否是回文串,然后把后面的弄到前面即可,不过这样仅仅能擦边通过测试,最高效的当然是KMP算法了,

KMP可以参考Java for LeetCode 028 Implement strStr()

JAVA实现如下;

    public String shortestPalindrome(String s) {
for(int i=s.length();i>=1;i--)
if(isPalindrome(s.substring(0, i)))
return new StringBuilder(s.substring(i)).reverse()+s;
return "";
}
static boolean isPalindrome(String s){
int left=0,right=s.length()-1;
while(left<right)
if(s.charAt(left++)!=s.charAt(right--))
return false;
return true;
}

Java for LeetCode 214 Shortest Palindrome的更多相关文章

  1. [LeetCode] 214. 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. 【LeetCode】214. Shortest Palindrome 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀是否回文 判断前缀 相似题目 参考资料 日期 题 ...

  4. 【LeetCode】214. Shortest Palindrome

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

  5. 214. Shortest Palindrome

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

  6. 【leetcode】Shortest Palindrome(hard)★

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

  7. 【Leetcode】Shortest Palindrome

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

  8. Java for LeetCode 125 Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. 214 Shortest Palindrome 最短回文串

    给一个字符串 S, 你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串.例如:给出 "aacecaaa",返回 "aaacecaaa ...

随机推荐

  1. strtr函数的用法

    http://php.net/manual/en/function.strtr.php <?php $trans = array("h" => "-" ...

  2. matlab求解相关系数

    最近收到一项新任务,要求两个矩阵的相关系数,说白了就是转换成向量两两计算.本来这个工作我是想自己写个小程序搞定的,但是大家纷纷反映matlab自带了此项功能,本着活到老学到老的心态,我开始查找这个函数 ...

  3. GOF业务场景的设计模式-----策略模式

    定义:定义一组算法,将每个算法都封装起来,并且使他们之间可以互换. 策略模式代码实现 interface IStrategy { public void doSomething(); } class ...

  4. autopep8

    修正python pep8的警告挺无趣的, 用了 autopep8 感觉比较爽. 记录如下. ----------------pep8检查----------------平时我用pydev做pep8检 ...

  5. 部分LINUX系统由图形界面启动变更为命令行界面启动的方法

    背景: 图形界面很绚丽,但是现在并不需要图形界面,只需要命令行即可,所以要将图形界面自启动给关闭.    正文:   Centos:     更改文件/etc/inittab的其中一行    id:5 ...

  6. eclipse工具背景色模板-程序员保护好自己的眼睛

    做为coder,要保护好自己的眼睛,eclipse 强烈推荐 Eclipse Color Theme插件,该插件包含多种当前流行的主题选择. 安装方法: 安装方法:1.先安装一个Eclipse Col ...

  7. linux 脚本命令匹配并获取下一行数据

    三种方式: 匹配“Title”并打印出匹配行的下一行 grep  -A 1 'Title'  urfile awk '/Title/{getline a;print $0"\n"a ...

  8. 类调用类的protected或private的成员函数或成员变量

    1.在其中一个类定义友元函数,则可以实现该类直接使用另外类的里所有内容. 一般实例化两个类,友元类以及自身类,实现友元类传递指针到自身类 2.如果两个类是可以继承的关系,则在子类里继承该类,实现在子类 ...

  9. .net获取周几(中文)

    DateTime.Now.ToString("yyyy年MM月dd日 星期ddd hh时mm分ss秒", new System.Globalization.CultureInfo( ...

  10. PYTHON 自动化学习之路

    一.用户交互小程序 username = 'gyc' password = 'gyc' u = input("what is you name?:") p = input(&quo ...