Leetcode 214.最短回文串
最短回文串
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
示例 1:
输入: "aacecaaa"
输出: "aaacecaaa"
示例 2:
输入: "abcd"
输出: "dcbabcd"
这个题目是在字符串前面加字符构成一个最短的回文字符串。我们分析题意,就是找到从第一个字母起始的最长的回文字符串,然后把剩下的倒置加到前面,就得到了最短的回文字符串。怎么找到以第一个字母为起始的最长的回文串,我们可以把s转置,然后找到匹配转置后的字符串从原始字符串第一个开始位置能够匹配的最大长度。这时候我们可以考虑到使用KMP算法,因为KMP算法就是从目标字符串的第一个字母开始匹配。我们可以这么做
S+"#"+S.reverse 然后使用KMP算法
class Solution{
public String shortestPalindrome(String s){
String tmp=s+"#"+new StringBuilder(s).reverse().toString();
int[] table=getTable(tmp);
return new StringBuilder(s.substring(table[table.length-1])).reverse().toString()+s;
}
//KMP求next数组
public int[] getTable(String s){
int len=s.length();
int[] table=new int[len];
char[] a=s.toCharArray();
for(int i=1,k=0;i<len;i++){
while(k>0&&a[i]!=a[k]){
k=table[k-1];
}
if(a[i]==a[k]) k++;
table[i]=k;
}
return table;
}
}
Leetcode 214.最短回文串的更多相关文章
- Java实现 LeetCode 214 最短回文串
214. 最短回文串 给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出 ...
- leetcode 214. 最短回文串 解题报告
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...
- [python,2019-02-15] 最短回文串
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...
- [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. ...
- 214 Shortest Palindrome 最短回文串
给一个字符串 S, 你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串.例如:给出 "aacecaaa",返回 "aaacecaaa ...
- [Swift]LeetCode214. 最短回文串 | Shortest Palindrome
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- LeetCode:验证回文串【125】
LeetCode:验证回文串[125] 题目描述 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: ...
- 前端与算法 leetcode 125. 验证回文串
目录 # 前端与算法 leetcode 125. 验证回文串 题目描述 概要 提示 解析 解法一:api侠 解法二:双指针 算法 传入测试用例的运行结果 执行结果 GitHub仓库 查看更多 # 前端 ...
随机推荐
- 数学 Codeforces Round #308 (Div. 2) B. Vanya and Books
题目传送门 /* 水题:求总数字个数,开long long竟然莫名其妙WA了几次,也没改啥又对了:) */ #include <cstdio> #include <iostream& ...
- win10系统下使用EDGE浏览器找不到Report Builder 启动图标
Win10系统下如果要使用Report Builder,可能存在EDGE浏览器或者Chrome找不到ReportBuilder的启动图标的情况,此时,应以管理员权限运行IE浏览器,即可看到图标.
- Android SQLite(2)如何判断表是否已经存在
在sql语句中用 if not exists void create_table(){ SQLiteDatabase dbWireter = dbhelper.getWritableDatabase( ...
- js连续赋值,你理解了吗
看一道有意思的题,也许你会自信满满地写下答案,会是正确的吗? }; var b = a; a.x = a = {n: }; console.log('a',a); console.log('b',b) ...
- [转]Php MySql Class
本文转自:http://www.cnblogs.com/noevil/archive/2010/11/06/1870864.html <?php /** * 数据库操作类 * * @aut ...
- .net 发送邮件验证码
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Ma ...
- 教你如何在实战项目中使用WCF
我们都知道调用WCF直接在Service References中引用可以远程调用的WCF Url就行了. 但是我们想过没,在Development环境中可以这样做,但是QA.UAT.Productio ...
- math数学函数
Console.WriteLine("Math.Sign(12)--->{0})", Math.Sign(12)) Console.WriteLine("math. ...
- AJPFX关于通过索引获取最大值的思路
/*** 通过索引获取最大值***/public class Test1 { public static void main(String[] args) { ...
- MySQL学习随笔--视图
视图概念 数据库中的视图指的是一个虚拟表,其内容由查询定义.同真实的表一样,视图也是由行与列构成的.视图的数据来源由SQL语句查询得到,不存储数据 视图创建方法 格式 : create view 视图 ...