【LeetCode】214. Shortest Palindrome 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/shortest-palindrome/description/
题目描述
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.
Example 1:
Input: "aacecaaa"
Output: "aaacecaaa"
Example 2:
Input: "abcd"
Output: "dcbabcd"
题目大意
在一个字符串前面添加一些字符,使得整个字符串构成一个回文字符串。
解题方法
前缀是否回文
从后向前判断s字符串前面部分是不是一个回文字符串,如果是的话,就把后面的部分复制翻转一份到前面来,拼成了最短的回文字符串。
为什么从后向前,因为这样能使得前面部分的回文是最长的,所以总的回文长度是最短的。
有个长度是40002的特别长的字符串导致超时,所以我用了作弊的方法,就是直接返回它的结果,这样就加速了。
时间复杂度是O(n),空间复杂度是O(1).不作弊TLE,作弊超过100%.
class Solution:
def shortestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if len(s) > 40000: return 'a' * 20000 + "dc" + s
N = len(s)
for i in range(N, -1, -1):
if self.isPalindrome(s[:i]):
return s[i:][::-1] + s
return ""
def isPalindrome(self, s):
N = len(s)
for i in range(N // 2):
if s[i] != s[N - i - 1]:
return False
return True
判断前缀
先把字符串s进行翻转得到t,我们要判断s的前缀如果和t的等长度的后缀一样,那么说明他们两个拼在一起是个回文字符串。举个栗子:
s: (aacecaa)a
t: a(aacecaa)
a aacecaaa
时间复杂度是O(n),空间复杂度是O(n).Python3能过,python2会TLE,需要用作弊。
class Solution:
def shortestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
N = len(s)
if N == 0: return ""
t = s[::-1]
for i in range(N, 0, -1):
if s[:i] == t[N - i:]:
break
return t[:N - i] + s
相似题目
参考资料
https://zxi.mytechroad.com/blog/string/leetcode-214-shortest-palindrome/
日期
2018 年 11 月 2 日 —— 浑浑噩噩的一天
【LeetCode】214. Shortest Palindrome 解题报告(Python)的更多相关文章
- 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] 214. 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 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
随机推荐
- Selenium-IDE,在网页上模拟人的操作
想偷懒,不想做很机械重复的网页操作,就百度了一下看看有什么方法,能把自己从重复性的网页操作中解放出来,于是,百度到了selenium ide,折腾许久,用最新版火狐添加了自带selenium ide组 ...
- 完美png图片添加水印类
完美png图片添加水印类 被添加水印图片和水印图片都可以是png,保证透明无色背景,可调节透明度 <?phpclass Imgshuiyin{ /* 缩略图相关常量定义 */ const THU ...
- python 封装、绑定
目录 python 封装.绑定 1.数据.方法的封装 2.隐藏属性 3.开放接口 4.绑定方法 1.对象的绑定 2.类的绑定(classmethod) 3.非绑定方法(staticmethod) 4. ...
- The Go tools for Windows + Assembler很好玩
我想用python做个tiny BASIC编译器.赋值和加减乘除,IF和FOR. 语法分析python有ply包,用ply.lex和ply.yacc做个计算器很简单,我已经做了. 做个解释器应该也不难 ...
- A Child's History of England.5
Above all, it was in the Roman time, and by means of Roman ships, that the Christian Religion was fi ...
- accomplish, accord
accomplish =achieve; accomplishment=achievement. accomplished: well educated/trained, skilled. skill ...
- day11 函数
day11 函数 一.函数基础 """ 1 什么是函数 函数是盛放代码的容器:把实现某一功能的代码放到一个函数内就制造一个工具 2 为何要用函数 没有用函数之前程序的问题 ...
- 数仓day04----日志预处理2
1.详细描述idmap的整个计算方案 (1)使用SparkSession对象读取用户不同类别的埋点日志,解析并抽取出相应的标识id,使用union进行合并,得到装有汇总标识id的rdd(ids) (2 ...
- 【Reverse】每日必逆0x03
BUU-刮开有奖 附件:https://files.buuoj.cn/files/abe6e2152471e1e1cbd9e5c0cae95d29/8f80610b-8701-4c7f-ad60-63 ...
- 【leetcode】952. Largest Component Size by Common Factor(Union find)
You are given an integer array of unique positive integers nums. Consider the following graph: There ...